更新时间:2023-02-02 13:03:01
如果你调用Thread.setDefaultUncaughtExceptionHandler()的情况下,你的应用程序崩溃的uncaughtException()将百达进入。 强制关闭将不会出现和应用将是反应迟钝,这是不是一个非常好的事情。 为了重新启动应用程序时坠毁,你应该做以下的事情:
In case you call Thread.setDefaultUncaughtExceptionHandler() will allways enter in uncaughtException() in case your application crashed. "Force close" will not appear and the application will be unresponsive, which is not a quite good thing. In order to restart your application when it crashed you should do the following thing :
在onCreate方法,在你的主要活动初始化一个PendingIntent成员:
In onCreate method, in your main activity initialize a PendingIntent member:
intent = PendingIntent.getActivity(YourApplication.getInstance().getBaseContext(), 0,
new Intent(getIntent()), getIntent().getFlags());
比你的uncaughtException()方法:
Than in your uncaughtException() method:
AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, intent);
System.exit(2);
在不调用System.exit()将无法正常工作。这code将在2秒后重新启动应用程序。
Without calling System.exit() will not work. This code will restart your application after 2 seconds.
最后,你可以设置一些标志你的意图,该应用程序崩溃,并在你的的onCreate()
方法,你可以显示一个对话框,对不起,该应用程序崩溃,希望永远不要再:)。
Eventually you can set some flag in your intent that the application crashed and in your onCreate()
method you can show a dialog "I'm sorry, the application crashed, hope never again :)".