クラッシュ後に Android アプリケーションを再起動したい。私の問題は、次のメソッドを呼び出して手動でアプリケーションを閉じたいときにも再起動することです:finish();
PendingIntent _pendingInt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.readings_main);
//create pending intent, which starts the Application
this._pendingInt=PendingIntent.getActivity(MyApplication.getInstance().getBaseContext(), 0,
new Intent(getIntent()), getIntent().getFlags());
// start handler which starts pending-intent after Application-Crash
Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler(this._pendingInt, this.getApplicationContext()));
}
そして私の CustomExceptionHandler:
public class CustomExceptionHandler implements UncaughtExceptionHandler {
private PendingIntent _penIntent;
Context cont;
/**
* Constructor
* @param intent
* @param cont
*/
public CustomExceptionHandler(PendingIntent intent, Context cont) {
this._penIntent = intent;
this.cont=cont;
}
public void uncaughtException(Thread t, Throwable e) {
AlarmManager mgr = (AlarmManager) cont.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 60000, this._penIntent);
System.exit(2);
}
}
そのため、Menu-Option finish() を介して呼び出すと、アプリケーションは 1 分後に開始されます。