Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : avoid crashing of app due to unhandled errors

In my Android app I have tried to put Try Catch blocks in all possible places. However I want to avoid the crashing of the app due to any unhandled errors. How can I achieve that?

I have used Thread.setDefaultUncaughtExceptionHandler(handler); but that will just help me to obtain crash- data right?

like image 585
Tushar Vengurlekar Avatar asked May 27 '11 14:05

Tushar Vengurlekar


2 Answers

you can use the following way :

public class MyApplication extends Application
{
  public void onCreate ()
  {
    // Setup handler for uncaught exceptions.
    Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
    {
      @Override
      public void uncaughtException (Thread thread, Throwable e)
      {
        handleUncaughtException (thread, e);
      }
    });
  }

 // here you can handle all unexpected crashes 
  public void handleUncaughtException (Thread thread, Throwable e)
  {
    e.printStackTrace(); // not all Android versions will print the stack trace automatically

    Intent intent = new Intent ();
    intent.setAction ("com.mydomain.SEND_LOG"); // see step 5.
    intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK); // required when starting from Application
    startActivity (intent);

    System.exit(1); // kill off the crashed app
  }
}

that will handle your app unexpected crashes, this taken from that answer.

like image 161
Muhammed Refaat Avatar answered Oct 21 '22 02:10

Muhammed Refaat


I suggest you read about ACRA here

like image 35
mah Avatar answered Oct 21 '22 02:10

mah