Android development Application global exception handling

When doing android project development, everyone knows that if the program goes wrong, a forced exit pop-up box will pop up. This is no problem in itself, but this UI is really ugly, let alone the user can’t accept it, even ourselves. It may not be acceptable. Although we always go through careful testing when we release the program, we will inevitably encounter unexpected errors.

Today, I will customize the handling of a program error, similar to the crash of iphone. (Although the flashback is not what users want to see, it is obviously much better than the native pop-up in terms of user experience)

Not much nonsense, just go to the code:

CrashHandler

/** 
 * 自定义的 异常处理类 , 实现了 UncaughtExceptionHandler接口  
 * 
 */  
public class CrashHandler implements UncaughtExceptionHandler {
      
        
    // 需求是 整个应用程序 只有一个 MyCrash-Handler   
    private static CrashHandler INSTANCE ;  
    private Context context;  
      
    //1.私有化构造方法  
    private CrashHandler(){
      
        
          
    }  
      
    public static synchronized CrashHandler getInstance(){
      
        
        if (INSTANCE == null)  
            INSTANCE = new CrashHandler();  
        return INSTANCE;
    }

    public void init(Context context){
      
        
        this.context = context;
    }  
      
  
    public void uncaughtException(Thread arg0, Throwable arg1) {
      
        
        System.out.println("程序挂掉了 ");  
        // 在此可以把用户手机的一些信息以及异常信息捕获并上传,由于UMeng在这方面有很程序的api接口来调用,故没有考虑
          
        //干掉当前的程序   
        android.os.Process.killProcess(android.os.Process.myPid());  
    }  

}

CrashApplication

/** 
 * 在开发应用时都会和Activity打交道,而Application使用的就相对较少了。 
 * Application是用来管理应用程序的全局状态的,比如载入资源文件。 
 * 在应用程序启动的时候Application会首先创建,然后才会根据情况(Intent)启动相应的Activity或者Service。 
 * 在本文将在Application中注册未捕获异常处理器。 
 */  
public class CrashApplication extends Application {
      
        
    @Override  
    public void onCreate() {
      
        
        super.onCreate();  
        CrashHandler handler = CrashHandler.getInstance();  
        handler.init(getApplicationContext());
        Thread.setDefaultUncaughtExceptionHandler(handler);  
    }  
}

Register in AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="org.wp.activity" android:versionCode="1" android:versionName="1.0">  
    <application android:icon="@drawable/icon" android:label="@string/app_name"  
        android:name=".CrashApplication" android:debuggable="true">  
        <activity android:name=".MainActivity" android:label="@string/app_name">  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />  
                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
        </activity>  
    </application>  
    <uses-sdk android:minSdkVersion="8" />  
</manifest>

At this point, you can test that when an error occurs, the program will directly flash back and kill the background process. Of course, you can also customize some friendly error UI prompts to further enhance the user experience.


    Guess you like

    Origin blog.csdn.net/xhf_123/article/details/49932767