1.Application中设置崩溃回调方法
package com.exam.crash;
import android.app.Application;
import android.util.Log;
public class App extends Application
{
@Override
public void onCreate()
{
super.onCreate();
Log.i("App", "onCreate---------------------");
Thread.setDefaultUncaughtExceptionHandler(new DefaultCrashExceptionHandler());
}
}
2.设置UncaughtExceptionHandler
import java.lang.Thread.UncaughtExceptionHandler;
import android.content.Context;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;
public class DefaultCrashExceptionHandler implements UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread thread, Throwable tr) {
Log.e("DefaultCrashExceptionHandler", "uncaughtException", tr);
new Thread() {
@Override
public void run() {
Looper.prepare();
Context context = PhoneMstMng.getContext();
if (context != null) {
Toast.makeText(context, "程序异常,即将退出", Toast.LENGTH_LONG)
.show();
}
Looper.loop();
}
}.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
Log.e("DefaultCrashExceptionHandler", "InterruptedException", e);
}
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
}