您的位置:首页 > 移动开发

关于如何获取上线后的app异常信息(bug)处理

2016-11-24 16:11 591 查看
首先是在Application中实现接口

public class App extends Application {
//记得在清单文件注册App的name
@Override
public void onCreate() {
super.onCreate();
Thread.setDefaultUncaughtExceptionHandler(new MYExceptionHandler());
}
/**
* 当app出现异常就会走下面的方法!!!
* */
class  MYExceptionHandler implements Thread.UncaughtExceptionHandler{

@Override
public void uncaughtException(Thread thread, Throwable throwable) {
if (throwable != null) {
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
throwable.printStackTrace(printWriter);
String errorReport = result.toString();
Log.d("-<<<<>" , errorReport);

//这里吧errorReport post到http去就行了!!!
//...

//或者保存文件到sd卡中
saveInFile(errorReport );

//最后杀死自己             android.os.Process.killProcess(android.os.Process.myPid());

}
}
}
private void saveInFile(String errorReport) {
FileOutputStream out = null ;

File  file = new File(Environment.getExternalStorageDirectory() , "error-log.txt");
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
try {
out = new FileOutputStream(file);
out.write(errorReport.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (out != null) try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}


我这个项目是如果异常了,就再次启动自己

//在这个方法里启动另一个activity
public void uncaughtException(Thread thread, Throwable ex) {
if (ex != null) {
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
ex.printStackTrace(printWriter);
String errorReport = result.toString();
Log.d("-<<<<>" , errorReport);

//上传http

Intent intent = new Intent("com.dopool.devinstall.TwoActivity");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(CATCH_EXCEPTION, errorReport);
mContent.startActivity(intent);
android.os.Process.killProcess(android.os.Process.myPid());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  异常