崩溃后重新启动应用程序无效

时间:2014-04-07 14:42:49

标签: android crash restart

崩溃发生后我需要重启我的应用程序。我的代码不起作用。我错过了什么吗?

import android.content.Context;
import android.content.Intent;

public class MyApplication implements java.lang.Thread.UncaughtExceptionHandler {
    private final Context myContext;

    public MyApplication(Context context) {
        myContext = context;
    }

    public void uncaughtException(Thread thread, Throwable exception) {
        Intent intent = new Intent(myContext, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        myContext.startActivity(intent);

        System.exit(10);
    }
}

1 个答案:

答案 0 :(得分:0)

我告诉你如何simulate重启,但你真的,非常不应该这样做!

    Intent mStartActivity = new Intent(getActivity(), YourActivity.class);
    int mPendingIntentId = 123453456;
    PendingIntent mPendingIntent = PendingIntent.getActivity(getActivity(),
            mPendingIntentId, mStartActivity,
            PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager mgr = (AlarmManager) getActivity().getSystemService(
            Context.ALARM_SERVICE);
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100,
            mPendingIntent);
    System.exit(0);

同样,你应该找到一个更清洁的解决方案。