1

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.

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. i am not able to identify where i have done mistake.Any help is appreciated.

Hardik Mehta
  • 867
  • 1
  • 12
  • 20

5 Answers5

1

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.

Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
  • 1
    @ Chintan Rathod thanks for your response i have visited this tutorial but the problem is my activty is not starting again. i also tried to remove android.os.Process.killProcess(android.os.Process.myPid()); System.exit(0); but still is not restarting. – Hardik Mehta Apr 14 '17 at 05:12
  • @ Chintan Rathod yup but it is not working. splash screen is my launcher activity.can it could be cause for not restarting. – Hardik Mehta Apr 14 '17 at 05:47
  • @HardikMehta i edited my answer. Check and try again. Try to implement same like tutorial without adding any extra checks and code. – Chintan Rathod Apr 14 '17 at 05:51
  • @ Chintan Rathod i tried but it is not working. i think i have done something wrong.let me edit my question. – Hardik Mehta Apr 14 '17 at 06:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/141696/discussion-between-hardik-mehta-and-chintan-rathod). – Hardik Mehta Apr 14 '17 at 06:24
1

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:

Thread.setDefaultUncaughtExceptionHandler(new DefaultExceptionHandler(this));
Shrikant
  • 247
  • 1
  • 13
1

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);
Waqas Ahmed Ansari
  • 1,683
  • 15
  • 30
0

use getContext() instead of getApplicationContext() or 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 :)

Muhammad Saad Rafique
  • 3,158
  • 1
  • 13
  • 21
0

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);
}
Minkoo
  • 439
  • 1
  • 3
  • 16