简体   繁体   English

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

[英]Restart Android Application after crash

i want to restart my Android Application after a crash. 我想在崩溃后重新启动我的Android应用程序。 My Problem is that it also restarts when i want to close the application manually by calling the method: 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()));
}

And my CustomExceptionHandler: 还有我的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);
}
}

So, when i call through Menu-Option finish(), the Application starts after 1 min. 因此,当我通过Menu-Option finish()调用时,应用程序将在1分钟后启动。

To be clear, you want to restart the app if it crashes, but you don't want to restart it if you close it manually. 需要明确的是,您希望在崩溃时重新启动该应用程序,但是如果您手动关闭它,则不想重新启动它。 Did I get this right? 我说对了吗?

I'm not an expert on this, but my guess is that specifying a custom exception handler is delaying the system from wiping out your app until it can call onDestroy(). 我不是专家,但是我的猜测是,指定自定义异常处理程序会延迟系统清除您的应用程序,直到可以调用onDestroy()为止。 This means that you go through the same lifecycle regardless of how your Activity ends. 这意味着无论您的活动如何结束,您都将经历相同的生命周期。

Perhaps there's a way to get around this. 也许有办法解决这个问题。 You can detect that your app is finishing by calling onFinishing() in onPause(). 您可以通过在onPause()中调用onFinishing()来检测您的应用是否正在完成。 If you disable the customExceptionHandler at that point, it won't try to restart the app. 如果此时禁用customExceptionHandler,它将不会尝试重新启动应用程序。

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

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