繁体   English   中英

崩溃后重新启动Android应用程序

[英]Restart Android Application after crash

我想在崩溃后重新启动我的Android应用程序。 我的问题是,当我想通过调用以下方法手动关闭应用程序时,它也会重新启动:

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分钟后启动。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM