Displaying a Toast message from the UncaughtExceptionHandler

412 views Asked by At

I am trying to show a Toast message from a class which is a customExceptionHandler. But I am unable to do that.

I have seen the similar issue like Displaying a Toast message from the Application class But still, it didn't solve my issue. Is anything I'm missing here.

I can see the Log statement in Logcat but the toast is not showing up

public class MyApplication extends Application {

    private Thread.UncaughtExceptionHandler defaultUEH;

    private Thread.UncaughtExceptionHandler unCaughtExceptionHandler =
            new Thread.UncaughtExceptionHandler() {
                @Override
                public void uncaughtException(Thread thread, final Throwable ex) {
                    Log.e("Inside Run", "******************** Inside uncaughtException ***************" + ex.getMessage());
                    Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_SHORT).show();
                }
            };

    public MyApplication() {
        defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(unCaughtExceptionHandler);
    }

}

I have tried one more thing that, I know static methods and variables are not an optimal solution, but I have created a static method in a util class maintained activitiy context in a static variable and used that to show a toast that one also didn't work

Like

Util.showToast(ex.getMessage()); 

in the uncaughtException() method

3

There are 3 answers

4
ND1010_ On

If you want to show Toast in your Application calss than it must have a context to show Toast so you can pass Context in your constructor or get instance of activity

I have not try this code but you should try this:

private static ApplicationContext instance;
/**
 * Your Constructor
 */
public MyApplication() {
    instance = this;
}

/**
 * Gets the application context.
 * @return the application context
 */
public static Context getContext() {
    if (instance == null) {
        instance = new ApplicationContext();
    }
    return instance;
}

       public static void showToast(String data) {
        Toast.makeText(getContext(), data,
                Toast.LENGTH_SHORT).show();
    }
2
AskNilesh On

Android Manifest file, declare the following.

 <application android:name="com.example.MyApplication">

    </application>

Then write the class:

public class MyApplication extends Application {

    private static Context context;

    public void onCreate() {
        super.onCreate();
        MyApplication.context = getApplicationContext();
    }

    public static Context getAppContext() {
        return MyApplication.context;
    }
}

Now everywhere call MyApplication.getAppContext() to get your application context statically.

0
Дмитрий Грибков On

This work for me

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread paramThread, Throwable paramThrowable) {

            new Thread() {
                @Override
                public void run() {
                    Looper.prepare();
                    Toast.makeText(getActivity(),"Your message", Toast.LENGTH_LONG).show();
                    Looper.loop();
                }
            }.start();
            try
            {
                Thread.sleep(3000); // Let the Toast display before app will get shutdown
            }
            catch (InterruptedException e) {    }
            System.exit(2);
        }
    });