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

    xiaoxiao2021-12-12  4

    首先是在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());
    转载请注明原文地址: https://ju.6miu.com/read-900249.html

    最新回复(0)