1

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

4

1 回答 1

1

需要明确的是,如果应用程序崩溃,您想重新启动它,但如果您手动关闭它,您不想重新启动它。我做对了吗?

我不是这方面的专家,但我的猜测是,指定自定义异常处理程序会延迟系统清除您的应用程序,直到它可以调用 onDestroy()。这意味着无论您的活动如何结束,您都会经历相同的生命周期。

也许有办法解决这个问题。您可以通过在 onPause() 中调用 onFinishing() 来检测您的应用程序正在完成。如果此时禁用 customExceptionHandler,它将不会尝试重新启动应用程序。

于 2013-09-16T23:18:02.993 回答