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?
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?
The trick is make sure it doesn't Force Close in the first place.
If you use the Thread.setDefaultUncaughtExceptionHandler()
method you can catch the Exceptions that are causing your application to Force Close.
Have a look at this question for an example of using an UncaughtExceptionHandler
to log the Exceptions raised by an application.
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 :
In onCreate method, in your main activity initialize a PendingIntent member:
intent = PendingIntent.getActivity(YourApplication.getInstance().getBaseContext(), 0,
new Intent(getIntent()), getIntent().getFlags());
Than in your uncaughtException() method:
AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, intent);
System.exit(2);
Without calling System.exit() will not work. This code will restart your application after 2 seconds.
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 :)".