繁体   English   中英

崩溃后应用程序重启

[英]Application restart after crash

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

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);

        }

    }

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

查看本教程 我也在使用此代码片段崩溃后重新启动我的应用程序。

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);

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

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

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));

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

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

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

我尽力找到解决方案,我总是使用下面的代码:

@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);
}

暂无
暂无

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

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