1、登录腾讯bugly平台创建应用
腾讯Bugly - 一种愉悦的开发方式 _android anr_android anr分析_iOS崩溃日志分析平台
2、1加载依赖
/*崩溃处理 */
implementation 'com.tonystark.android:defense_crash:2.0.0'
//bugly
implementation 'com.tencent.bugly:crashreport_upgrade:1.3.5'implementation'com.tencent.bugly:nativecrashreport:3.3.1'
2、2添加maven
maven { url 'https://dl.bintray.com/xuuhaoo/maven/' }
3、工具类
public class ForReportException extends Exception {public ForReportException() {}public ForReportException(String msg) {super(msg);}public ForReportException(String msg, Throwable throwable) {super(msg, throwable);}
}
4、实现捕获异常上报代码
public class App extends Application implements IExceptionHandler {
initBugly();
}private void initBugly() {CrashReport.initCrashReport(getApplicationContext(), "c50f1d7329", BuildConfig.DEBUG);
}
@Override
protected void attachBaseContext(Context base) {super.attachBaseContext(base);/**///MultiDex.install(this);// step1: Initialize the lib.DefenseCrash.initialize();// step2: Install the fire wall defense.DefenseCrash.install(this);
}@Override
public void onCaughtException(Thread thread, Throwable throwable, boolean b) {throwable.printStackTrace();/*todo 收集系统信息*//*todo 发送邮件*//*bugly 手动上报异常*/CrashReport.postCatchedException(throwable);
}@Override
public void onEnterSafeMode() {LogPlus.d("###########onEnterSafeMode###########");
}@Override
public void onMayBeBlackScreen(Throwable throwable) {/*重启app*/Thread thread = Looper.getMainLooper().getThread();CrashReport.postCatchedException(new ForReportException("onMayBeBlackScreen", throwable));restartApp();
}/*** 重启app*/
public void restartApp() {Intent mStartActivity = new Intent(this, MainActivity.class);int mPendingIntentId = 123456;PendingIntent mPendingIntent = PendingIntent.getActivity(this, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);AlarmManager mgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);System.exit(0);
}
主动抛出异常
throw new IllegalStateException("setDefaultFrameFormat should be call before initMonitor");
5、登录bugly平台即可查看到所有APP的异常日志,定位去解决效果
String sendstring = stringBuffer.toString();Throwable throwable = new Throwable(sendstring);CrashReport.postCatchedException(throwable);//上报到bugly
@Override public void onCaughtException(Thread thread, Throwable throwable, boolean b) {CrashReport.postCatchedException(throwable); }@Override public void onEnterSafeMode() {}@Override public void onMayBeBlackScreen(Throwable throwable) {CrashReport.postCatchedException(throwable);/*重启APP*/restartApp(); }/*** 重启app*/ public void restartApp() {Intent mStartActivity = new Intent(this, SplashScreenActivity.class);int mPendingIntentId = 123456;PendingIntent mPendingIntent = PendingIntent.getActivity(this, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);AlarmManager mgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);System.exit(0); }
实现demo:android开发,异常处理,捕获异常上传到bugly,捕获异常库module-Android文档类资源-CSDN下载
bug交流:QQ 1085220040