0
package com.greenway.primemobile.common;

import java.io.PrintWriter;
import java.io.StringWriter;

import com.greenway.primemobile.login.LoginActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.widget.Toast;

public class ExceptionHandler implements java.lang.Thread.UncaughtExceptionHandler
{
    private final Context myContext;

    public ExceptionHandler(Context context) {
        myContext = context;
    }

    public void uncaughtException(Thread thread, Throwable exception) {             

        StringWriter stackTrace = new StringWriter();
        exception.printStackTrace(new PrintWriter(stackTrace));
        System.err.println(stackTrace);

        Intent intent = new Intent(myContext, ClassName.class);
        myContext.startActivity(intent);
        System.exit(0);

    }
}

The above class get's called when force-close happens, but how can I show a dialog or toast message to user that the application has crashed. Since its run on a different thread, I am not sure about how to add one.

Delimitry
  • 2,987
  • 4
  • 30
  • 39
theJava
  • 14,620
  • 45
  • 131
  • 172
  • i suggest not to use System.exit(0) . Check the answer by commonsware in the link http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon – Raghunandan May 15 '13 at 15:51
  • @Raghunandan: finish() is not working there either. – theJava May 15 '13 at 15:56
  • finish is for activity. i am not sure but when crash happens i guess app process is killed. – Raghunandan May 15 '13 at 15:57
  • http://stackoverflow.com/questions/16458770/is-onpause-guaranteed-to-be-called-when-an-activity-is-no-longer-running/16458844#16458844. check the answer by Raghav Sood. When crash happens your entire app process ceases to exist, inclusive of all threads you have created (UI or otherwise). So there is no point in creating a Exception Handler class if crash happens – Raghunandan May 15 '13 at 15:59

0 Answers0