简体   繁体   English

崩溃后应用程序重启

[英]Application restart after crash

I have implemented default exception thread handler in application as follows 我在应用程序中实现了默认异常线程处理程序如下

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


}

below is the code for SplashScreen. 下面是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);

        }

    }

and for test i throws a null pointer exception to test it from another activity.but after crash it is not restart flow from splash screen although i have set to redirect to splash screen. 并且对于测试我抛出一个空指针异常来从另一个activity测试它。但是在崩溃之后它不是从启动屏幕重启流,尽管我已经设置重定向到启动画面。 i am not able to identify where i have done mistake.Any help is appreciated. 我无法确定我在哪里犯了错误。感谢任何帮助。

Check this tutorial . 查看本教程 I am also restarting my application after crash with this code snippet. 我也在使用此代码片段崩溃后重新启动我的应用程序。

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

Edit 编辑

Just remove below line of code and try 只需删除下面的代码行,然后尝试

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

also remove check condition 还删除检查条件

if(Validator.isNotNull(activity)) {

Add below line of code. 添加以下代码行。

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

As your application is already stopped due to crash, you don't need to explicitly kill your process. 由于您的应用程序已因崩溃而停止,因此您无需明确终止您的进程。

This my code snippet to restart my app after crash happens 我的代码片段在崩溃后重启我的应用程序

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

}

} }

And from any activity's onCreate(), I register for Thread.UncaughtExceptionHandler like: 从任何活动的onCreate(),我注册Thread.UncaughtExceptionHandler,如:

Thread.setDefaultUncaughtExceptionHandler(new DefaultExceptionHandler(this));

I think the problem is in restarting the app. 我认为问题出在重启应用程序。 Try the code snippet below for restarting 请尝试下面的代码段重新启动

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

use getContext() instead of getApplicationContext() or getBaseContext(). 使用getContext()而不是getApplicationContext()或getBaseContext()。 I think you are not passing the right context. 我认为你没有传递正确的背景。 Thats the issue. 多数民众赞成的问题。 Check it if it helps do let me know. 检查它是否有帮助让我知道。 Best of luck :) 祝你好运:)

I tried my best finding out the solution and I always use the code below : 我尽力找到解决方案,我总是使用下面的代码:

@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