Android的:如何自动重新启动应用程序后,它已经与QUOT;强制关闭"? [英] Android: How to auto-restart application after it's been "force closed"?
问题描述
在一个Android应用程序,我们通常有强制关闭的错误,如果我们没有得到例外的权利。
In an Android application, we usually got the "Force Closed" error if we didn't get the exceptions right.
我怎样才能重新启动我的应用程序,如果自动强制关闭?
How can I restart my application automatically if it force closed?
有没有什么具体的权限用于此?
Is there any specific permission is used for this?
推荐答案
如果你调用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 :)".
这篇关于Android的:如何自动重新启动应用程序后,它已经与QUOT;强制关闭"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!