4

I want to handle the crashes smoothly ,for that i am restarting the app for every uncaught exceptions but there is always alert box is there "unfortunately ,xyz has stopped working" code that i am using to handle uncaught exceptions :

intent = PendingIntent.getActivity(this.getApplication().getBaseContext(), 0,
                new Intent(getIntent()), getIntent().getFlags());

        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread paramThread,
                    Throwable paramThrowable) {
                Log.e("Alert", "Lets See if it Works !!!");
        AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, intent);
                System.exit(0);

            }
Seshu Vinay
  • 13,560
  • 9
  • 60
  • 109
Harsh Middha
  • 143
  • 1
  • 8

4 Answers4

1

When you get "unfortunately ,xyz has stopped working" , always put some breakpoints and DEBUG your code properly.Check your LOGCAT, you will land up finding the problems you are facing when your app crashes. Thanks!

Shruti Dasgopal
  • 551
  • 4
  • 10
  • :- It is just in case my app crashes ,it should automatically restart and i want to disable the alert box which comes before app gets restarted – Harsh Middha Dec 04 '14 at 11:46
  • As mentioned in other comments, i too say that the alert box is done by the system. To be more clear, do you still want to restart activity after it crashes? I have one link regarding the same for you. Hope it works! http://stackoverflow.com/questions/2681499/android-how-to-auto-restart-application-after-its-been-force-closed – Shruti Dasgopal Dec 04 '14 at 16:08
1

Code:

public class BaseActivity extends Activity {
    @Override
    public void onCreate() {
        super.onCreate();

        final Thread.UncaughtExceptionHandler oldHandler =
            Thread.getDefaultUncaughtExceptionHandler();

        Thread.setDefaultUncaughtExceptionHandler(
            new Thread.UncaughtExceptionHandler() {
                @Override
                public void uncaughtException(
                    Thread paramThread,
                    Throwable paramThrowable
                ) {
                    //Do your own error handling here

                    if (oldHandler != null)
                        oldHandler.uncaughtException(
                            paramThread,
                            paramThrowable
                        ); //Delegates to Android's error handling
                    else
                        System.exit(2); //Prevents the service/app from freezing
                }
            });
    }
}
Q's
  • 70
  • 9
Yossi Gruner
  • 120
  • 7
0

The alert is done by the system, there isn't much you can do about it.

You should just catch an Exception when it is thrown and then let the flow of your app continue accordingly. Restarting your app every time an Exception occurs is anything but smooth.

Thorben
  • 1,019
  • 7
  • 20
  • ...i also dont want to see that alert box "unfortuantely your app stopped working " – Harsh Middha Dec 04 '14 at 11:43
  • Well that appears because you don't catch your exceptions properly. Find out where the exceptions are thrown and catch them. Then your app won't crash and the alert box will not be shown. – Thorben Dec 04 '14 at 11:46
  • This isn't the case for me. I have a kiosk-based rooted device and I need to suppress the message during an APK update. Do you have any pointers to suppress / change the system dialog? – Charlie Mar 16 '17 at 10:15
0
           @Override
           public void uncaughtException(Thread paramThread,
                           Throwable paramThrowable) {
                   Log.e("Alert", "Restarting app !!!");
                   Intent intent = getBaseContext().getPackageManager()
                        .getLaunchIntentForPackage( getBaseContext().getPac
           //i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
           intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTI
           startActivity(intent);
           System.exit(2);
           }
   });
Harsh Middha
  • 143
  • 1
  • 8