崩溃后应用程序重启

时间:2017-04-14 05:06:30

标签: java android exception exception-handling

我在应用程序中实现了默认异常线程处理程序,如下所示

public class ExceptionHandler implements Thread.UncaughtExceptionHandler {

    private Context context;
    private Class<?> activity;

    public ExceptionHandler(MobileTechnicianApp mobileTechnicianApp, Class<?>  activityClass) {
        context=mobileTechnicianApp.getApplicationContext();
        activity=activityClass;
    }






    @Override
    public void uncaughtException(Thread thread, Throwable exception) {
        if(Validator.isNotNull(activity)) {
            Intent intent=new Intent(context,SplashScreenActivity.class);
            intent.putExtra(Constants.CRASH_BUNDLE_KEY,"crash");
            context.startActivity(intent);
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
        }
    }


}

下面是SplashScreen的代码。

public class SplashScreenActivity extends GlobalActivity implements Responselistner {
    WeakHandler weakHandler;
    private AVLoadingIndicatorView progressView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);
        weakHandler=new WeakHandler();
        progressView= (AVLoadingIndicatorView) findViewById(R.id.text_dot_loader);
        progressView.setVisibility(View.VISIBLE);
        progressView.show();
        Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
        if(Validator.isNotNull(getIntent().getStringExtra(Constants.CRASH_BUNDLE_KEY))){
            if(Validator.isNotNull(getIntent().getStringExtra(Constants.CRASH_BUNDLE_KEY))){
                Request.makeJsonObjectRequest(Constants.URL_GET_REQUEST_STATUS,this, RequestParameterBuilder.buildParameterforRequestStatus());
            }
            else {
                Log.d("token","else");
            }
        }
        else {
            weakHandler.postDelayed(new Runnable(){
                @Override
                public void run() {
                    if (MobileTechnicianApp.preferences.getBoolean(Constants.PREFERENCE_MOBILE_NUMBER_VERIFIED_KEY, false)) {
                        redirect(ActivityOne.class);
                    } else {
                        redirect(AuthenticationOneActivity.class);
                    }
                }
            }, 5000);

        }

    }

并且对于测试我抛出一个空指针异常以从另一个活动测试它。但是在崩溃后它不是从启动屏幕重启流,尽管我已经设置重定向到启动画面。我无法确定我在哪里犯了错误。感谢任何帮助。

5 个答案:

答案 0 :(得分:1)

检查 this tutorial 。我也在使用此代码片段崩溃后重新启动我的应用程序。

PendingIntent pendingIntent = PendingIntent.getActivity(
    YourApplication.getInstance().getBaseContext(), 0,
            intent, intent.getFlags());

AlarmManager mgr = (AlarmManager) 
YourApplication.getInstance().getBaseContext()
            .getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, pendingIntent);

activity.finish();

System.exit(2);

修改

只需删除以下代码行,然后尝试

android.os.Process.killProcess(android.os.Process.myPid());

也删除检查条件

if(Validator.isNotNull(activity)) {

添加以下代码行。

@Override
protected void onCreate(Bundle savedInstanceState) {
    Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
    super.onCreate(savedInstanceState);

由于您的应用程序因崩溃而已停止,因此您无需明确终止您的进程。

答案 1 :(得分:1)

我认为问题出在重启应用程序。请尝试下面的代码段重新启动

Intent intent = getBaseContext().getPackageManager()
                        .getLaunchIntentForPackage(getBaseContext().getPackageName() );
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

答案 2 :(得分:0)

这是我在崩溃后重启我的应用程序的代码片段

public class DefaultExceptionHandler implements Thread.UncaughtExceptionHandler {

private Thread.UncaughtExceptionHandler defaultUEH;
Activity activity;

public DefaultExceptionHandler(Activity activity) {
    this.activity = activity;
}

@Override
public void uncaughtException(Thread thread, Throwable ex) {

    Intent intent = new Intent(activity, SplashActivity.class);

    intent.putExtra("crash",true);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_CLEAR_TASK
            | Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent pendingIntent = PendingIntent.getActivity(
            activity.getBaseContext(), 0, intent, intent.getFlags());

    //Following code will restart your application after 0.5 seconds
    AlarmManager mgr = (AlarmManager) activity.getBaseContext()
            .getSystemService(Context.ALARM_SERVICE);
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 500,
            pendingIntent);

    //This will finish your activity manually
    activity.finish();

    //This will stop your application and take out from it.
    System.exit(2);

}

}

从任何活动的onCreate()中,我注册了Thread.UncaughtExceptionHandler,如:

Thread.setDefaultUncaughtExceptionHandler(new DefaultExceptionHandler(this));

答案 3 :(得分:0)

使用getContext()而不是getApplicationContext()或getBaseContext()。我认为你没有传递正确的背景。多数民众赞成的问题。检查它是否有帮助让我知道。祝你好运:))

答案 4 :(得分:0)

我尽力找出解决方案,并且始终使用以下代码:

@Override
public void uncaughtException(Thread thread, Throwable throwable) {
    PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), 0,
            new Intent(this, GPCASplashActivity.class), PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    assert mgr != null;
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 500, pendingIntent);
    trackException(throwable);
    if (throwable != null) {
        try {
            Log.e("crash", throwable.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    System.exit(1);
}