1.自定义异常捕获类实现Thread.UncaughtExceptionHandler接口
public class CrashHandler implements Thread.UncaughtExceptionHandler {
private static CrashHandler crashHandler = new CrashHandler();
private CrashHandler() {
}
public static CrashHandler getInstance() {
return crashHandler;
}
public void init() {
Thread.setDefaultUncaughtExceptionHandler(this);
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
Log.e("zbw", ex.getMessage().toString());
}
}
2.自己写一个应用类继承Application
public class BaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
CrashHandler crashHandler = CrashHandler.getInstance();
crashHandler.init();
}
}
3.在清单文件中将Application的name改为自己应用类的类名
<application
android:name=".BaseApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>