Android removes annoying default flash-back dialog_android

Source: Internet
Author: User
Tags throwable

Android Application Flash Back there is always a "sorry, app has stopped running" window, so the user experience is not good. Many of the big companies in the app to the window, so this article mainly describes how to remove the default flashing window, and in the flash back to do some of the necessary clean-up work.

Uncaughtexceptionhandler
Uncaughtexceptionhandler can do some remedial work before the end of thread encounters the exception that live without catch. However, it does not stop the thread from running, and the thread eventually exits.

 Thread.setdefaultuncaughtexceptionhandler (New Thread.uncaughtexceptionhandler () {
   @Override public
   Void Uncaughtexception (thread thread, Throwable ex) {
    system.exit (1);
   }
  });

Remove dialog
The Android system defaults to a Uncaughtexceptionhandler, and the pop-up Flash window works in this handler. So if you want to remove the window, as long as the implementation of a Uncaughtexceptionhandler and replace the system by default, the code is as follows.

public class App extends application {


 @Override public
 void OnCreate () {
  super.oncreate ();
  Thread.setdefaultuncaughtexceptionhandler (New Myuncaughtexceptionhandler ());
 }



 Class Myuncaughtexceptionhandler implements thread.uncaughtexceptionhandler{

  @Override public
  Void Uncaughtexception (thread thread, Throwable ex) {
   ex.printstacktrace ();
   Do some work here

  android.os.Process.killProcess (Android.os.Process.myPid ());
   System.exit (1);
  }}}


The above replacement uncaughtexceptionhandler work is done uniformly in application, and you can do it in every activity, of course. For a single activity, it is necessary to implement a baseactivity when there is a lot of activity, replace it in baseactivity, and other activity to integrate baseactivity.

The necessary remedial work
in order to deal with the flash-back, improve the user experience, it is necessary to do some clean-up work, mainly a few listed as follows:

Exception escalation
you can use mail or upload via server interface. Both have advantages and disadvantages, the message is simple to develop, but requires additional user operations, poor user experience. If you use the Upload server method, because you cannot open a new thread in the Uncaughtexceptionhandler, you can only sync the request, and it will take a long time to block and run when the network situation is bad. It may also be reported as a result of a network failure. Of course, overall down or upload a server better. The concrete realization is left to the reader.

Record log
Store The Flash-back information in the file system. cannot be saved to sharedpreferences because opening the SP requires the use of a new thread (Android internal implementation), which is not allowed in Uncaughtexceptionhandler.

Flash back three times purge data
Most of the time is due to the background return data error caused by the flash-back. If the data is cached, then the user will be able to open the flash again, this time only by reloading or clearing the data in the way to solve the flash problem, the user experience is very bad. Therefore, it is necessary to automatically clear the cached data on multiple flash backs. Specific implementation can refer to my another blog Android implementation of multiple flash-back data removal. However, the blog used Acra, a uncaughtexceptionhandler again encapsulated open source project, readers can replace Acra Uncaughtexceptionhandler to achieve.

Turn on the app again
You can reopen the app in Uncaughtexceptionhandler or play out the definition window.

 Class Myuncaughtexceptionhandler implements thread.uncaughtexceptionhandler{

  @Override public
  Void Uncaughtexception (thread thread, Throwable ex) {
   ex.printstacktrace ();
   
   Intent Intent = new Intent (app.this, mainactivity.class);
   Intent.setflags (intent.flag_activity_new_task);
   App.this.startActivity (intent);

   Android.os.Process.killProcess (Android.os.Process.myPid ());
   System.exit (1);
  }
 }

Note SetFlags This step is required because the context used is the context of the app, so it is necessary to open a new task queue, otherwise the open activity cannot take effect if you replace the handler is done in the event, The context you get is the context of the activity, and you don't need this step.

Attention matters
the main point of attention I have mentioned earlier, do not open a new thread in the Uncaughtexceptionhandler, will throw an exception.

The above is the entire content of this article, I hope to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.