Question

I want to catch the uncaught exceptions and send reports about them to my server. This is how I implement it:

public final class MyApplication extends Application
{
    private static Context context;
    private static Handler mainHandler;
    private static boolean isDebug;

    @Override
    public void onCreate()
    {
        Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler()
        {
            @Override
            public void uncaughtException(final Thread thread, final Throwable ex)
            {
                new Thread()
                {
                    @Override
                    public void run()
                    {
                        Looper.prepare();

                        ReportUtils.sendReport(thread, ex);
                        Thread.currentThread().getUncaughtExceptionHandler().uncaughtException(thread, ex);
                        Looper.loop();
                        Looper.myLooper().quit();
                    }
                }.start();
            }
        });

        context = getApplicationContext();
        mainHandler = new Handler();
        isDebug = (0 != (getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE));
    }

    @Override
    public void onTerminate(){}

    public static Context getAppContext()
    {
        return context;
    }

    public static boolean isDebugMode()
    {
        return isDebug;
    }

    public static Handler getMainHandler()
    {
        return mainHandler;
    }
}

Is it the right way to implement it? Any other alternative?

Était-ce utile?

La solution

Instead of doing this yourself, use a service already setup to do it.

Acra is one, and can upload your crash reports to a Google Docs spreadsheet.

Bugsense is another. It has free accounts and paid accounts.

Crashlytics is rather new to android but is completely free, so I recommend it. It's really simple to setup.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top