Android: how to get started with UncaughtExceptionHandler installed in application

Possible duplicate:
How to get started with UncaughtExceptionHandler if this main thread crashes?

My goal is to display some error messages when my Android program crashes.

I have an exception handler registered in my onCreate software application (MyApplication):

Thread.setDefaultUncaughtExceptionHandler(new HelloUncaughtExceptionHandler()); 

And when the exception is caught, I would like to start a new activity for the user to save / send errors. This is what I am doing now:

 class HelloUncaughtExceptionHandler implements UncaughtExceptionHandler { @Override public void uncaughtException(Thread thread, Throwable e) { final Writer result = new StringWriter(); final PrintWriter printWriter = new PrintWriter(result); e.printStackTrace(printWriter); String stacktrace = result.toString(); printWriter.close(); Intent intent = new Intent(MyApplication.this, BugReportActivity.class); intent.putExtra("stacktrace", stacktrace); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } } 

I get a blank screen without BugReportActivity appearing (if I start it with activity, it works fine). "MyApplication.this" bothers me ... is there no way to trigger an arbitrary action from my application?

Thanks to everyone.

+6
source share

Source: https://habr.com/ru/post/891905/


All Articles