Android 捕获异常,上报异常日志,捕获奔溃日志,bugly使用实例

    技术2025-09-12  88

    1、登录腾讯bugly平台创建应用

     https://bugly.qq.com/

     

    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); }

     5、登录bugly平台即可查看到所有APP的异常日志,定位去解决效果

    String sendstring = stringBuffer.toString(); Throwable throwable = new Throwable(sendstring); CrashReport.postCatchedException(throwable);//上报到bugly

     

     

    Processed: 0.025, SQL: 9