我想在崩溃后重新启动我的 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 分钟后启动。