public class DemoApplication extends Application implements Thread.UncaughtExceptionHandler{
private PendingIntent pendingIntent;
@Override
public void onCreate() {
super.onCreate();
Thread.setDefaultUncaughtExceptionHandler(this);
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
Intent intent = new Intent(this, MainActivity.class);
pendingIntent= PendingIntent.getActivity(getApplicationContext(), -1, intent, (Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, 0 , pendingIntent);
System.exit(0);
}
程序发生运行时异常时,这个Application会自动捕捉异常,并重启启动app,亲测,ok