程序异常后自动

来源:互联网 发布:js实现select查询 编辑:程序博客网 时间:2024/04/30 04:14
public class DemoApplication extends Application implements Thread.UncaughtExceptionHandler{private PendingIntent pendingIntent;@Overridepublic void onCreate() {super.onCreate();Thread.setDefaultUncaughtExceptionHandler(this);}@Overridepublic 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


1 0
原创粉丝点击