No exceptions are captured during APP-level processing, while the APP-level processing captures
Preface:
The project APP sometimes has Crash, and then the system forced exit dialog box pops up. Click close app.
Some apps handle this and find that when an exception occurs in the program, Toast prompts "The program has an exception and will exit the program in 3 seconds ". After 3 seconds, the program will be closed without displaying the forced close dialog box.
So how do they handle exceptions that are not caught by try-catch and handle user-friendly prompt optimization.
Here we will learn through a demo.
Bytes ----------------------------------------------------------------------------------------------------------------
1. Create a CrashHandler class to implement the UncaughtExceptionHandler interface. This class handles exceptions that are not caught by the program.
Public class CrashHandler implements Thread. uncaughtExceptionHandler {private Thread. uncaughtExceptionHandler mDefaultHandler; Context context; // CrashHandler instance private static CrashHandler instance;/** obtain the CrashHandler instance, Singleton mode */public static CrashHandler getInstance () {if (instance = null) instance = new CrashHandler (); return instance;}/*** initialize */public void init (Context context) {this. context = context; // obtain the default UncaughtException processor mDefaultHandler = Thread. getDefaultUncaughtExceptionHandler (); // sets the CrashHandler as the default processor Thread of the program. setDefaultUncaughtExceptionHandler (this );}
// Implement the uncaughException method @ Override public void uncaughtException (Thread thread, Throwable ex) {if (! SolveException (ex) & mDefaultHandler! = Null) {// if the user does not process it, the system's default exception processor will handle mDefaultHandler. uncaughtException (thread, ex);} else {
// Wait 2 seconds before closing the program try {Thread. sleep (2000);} catch (InterruptedException e) {e. printStackTrace ();} android. OS. process. killProcess (android. OS. process. myPid (); System. exit (1) ;}/ ** handle errors **/private boolean solveException (Throwable e) {if (e = null) {return false;} new Thread () {@ Override public void run () {logoff. prepare (); Toast. makeText (context, "the program has an exception and exits after 2 seconds", Toast. LENGTH_SHORT ). show (); logoff. loop ();}}. start (); return true ;}}
2. Create a basic Application class MApplication
Initialize the onCreate () method
public class MApplication extends Application{ @Override public void onCreate() { super.onCreate(); CrashHandler.getInstance().init(getApplicationContext()); }}
Do not forget to add
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:name=".MApplication" android:theme="@style/AppTheme">
3. Simulate an exception
Assign a value to an unbound TextView. An error occurred while returning a null pointer.
public class MainActivity extends Activity { private TextView text; private TextView aa; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = (TextView) findViewById(R.id.text); text.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { text.setText("ni hao"); aa.setText("nihao "); } }); }}
:
Because we cannot give every method a try-catch. So there will always be any caught exceptions.
To improve the user experience.
Developers can also add exception analysis in this process to submit information about abnormal devices, causes, and time to their servers for further analysis.