总体步骤按照腾讯bugly官方文档为准
- 1.添加依赖
implementation 'com.tencent.bugly:crashreport:latest.release'
- 2.在清单文件中添加权限
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_LOGS"/>
- 3.创建一个捕获全局异常类 (名字自定义,最好见名识意)
package com.bw.ph;
import android.os.Build;
import android.os.Process;
import android.util.Log;
/**
* @Title:
* @Description:捕获全局异常类(以单例模式)
* @author: Mr.Pan
* @date: 2020-04-18 10:10
*/
public class MyUncaugExceptionHandler implements Thread.UncaughtExceptionHandler{
private static final String TAG = "MyUncaugExceptionHandle";
private static MyUncaugExceptionHandler inctance;
public MyUncaugExceptionHandler() {}
public static MyUncaugExceptionHandler getInctance() {
if (inctance == null) {
inctance=new MyUncaugExceptionHandler();
}
Thread.setDefaultUncaughtExceptionHandler(inctance);
return inctance;
}
@Override
public void uncaughtException(Thread t, Throwable e) {
Log.d(TAG, "uncaughtException: "+e.getMessage());
//收集异常信息
String model= Build.MODEL;
String brand= Build.BRAND;//手机品牌
int sdkInt = Build.VERSION.SDK_INT;//SDK版本
//打印日志
Log.d(TAG, "uncaughtException:== "+model);
Log.d(TAG, "uncaughtException:== "+brand);
Log.d(TAG, "uncaughtException:== "+sdkInt);
//杀死线程退出App 没有这行代码会卡死ANR
Process.killProcess(Process.myPid());
}
}
-
4.自定义Application 填写产品ID 例如下图 --- 在产品设置中找到
也可以在清单文件中配置产品ID 各选其一
dahui.png
package com.bw.ph;
import android.app.Application;
import com.tencent.bugly.crashreport.CrashReport;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
MyUncaugExceptionHandler.getInctance();
//第一个参数是上下文,第二个是注册时申请的APPID 第三个调试时设置为false 上市时设置为true
CrashReport.initCrashReport(getApplicationContext(), "21f6f4290b", true);
}
}
网友评论