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() を呼び出すことができるまでシステムがアプリを消去するのが遅れることになると思います。これは、Activity の終了方法に関係なく、同じライフサイクルをたどることを意味します。

おそらく、これを回避する方法があります。onPause() で onFinishing() を呼び出すことで、アプリが終了していることを検出できます。その時点で customExceptionHandler を無効にすると、アプリの再起動は試行されません。

于 2013-09-16T23:18:02.993 に答える