自己實現Android開發之全局異常捕獲

補充文章地址:安卓實現分業務模塊異常捕獲,全局異常不崩潰,應用繼續運行!

Android系統的手機版本和設備千差萬別,在模擬器上運行良好的程序安裝到某款手機上說不定就出現崩潰的現象,開發者個人不可能購買所有設備逐個調試,所以在程序發佈出去之後,如果出現了崩潰現象,開發者應該及時獲取在該設備上導致崩潰的信息,這對於下一個版本的bug修復幫助極大,所以今天就來介紹一下如何在程序崩潰的情況下收集相關的設備參數信息和具體的異常信息,併發送這些信息到服務器供開發者分析和調試程序。

源碼分析


    /**
     * Interface for handlers invoked when a <tt>Thread</tt> abruptly
     * terminates due to an uncaught exception.
     * <p>When a thread is about to terminate due to an uncaught exception
     * the Java Virtual Machine will query the thread for its
     * <tt>UncaughtExceptionHandler</tt> using
     * {@link #getUncaughtExceptionHandler} and will invoke the handler's
     * <tt>uncaughtException</tt> method, passing the thread and the
     * exception as arguments.
     * If a thread has not had its <tt>UncaughtExceptionHandler</tt>
     * explicitly set, then its <tt>ThreadGroup</tt> object acts as its
     * <tt>UncaughtExceptionHandler</tt>. If the <tt>ThreadGroup</tt> object
     * has no
     * special requirements for dealing with the exception, it can forward
     * the invocation to the {@linkplain #getDefaultUncaughtExceptionHandler
     * default uncaught exception handler}.
     *
     * @see #setDefaultUncaughtExceptionHandler
     * @see #setUncaughtExceptionHandler
     * @see ThreadGroup#uncaughtException
     * @since 1.5
     */
    @FunctionalInterface
    public interface UncaughtExceptionHandler {
        /**
         * Method invoked when the given thread terminates due to the
         * given uncaught exception.
         * <p>Any exception thrown by this method will be ignored by the
         * Java Virtual Machine.
         * @param t the thread
         * @param e the exception
         */
        void uncaughtException(Thread t, Throwable e);
    }

源碼是比較簡單的,通過源碼可以知道:
Thread.UncaughtExceptionHandler是一個當線程由於未捕獲的異常突然終止而調用處理程序的接口.
當線程由於未捕獲的異常即將終止時,Java虛擬機將使用它來查詢其UncaughtExceptionHandler的線程Thread.getUncaughtExceptionHandler(),並將調用處理程序的 uncaughtException方法,將線程和異常作爲參數傳遞。如果一個線程沒有顯示它的UncaughtExceptionHandler,那麼它的ThreadGroup對象充當它的 UncaughtExceptionHandler。如果ThreadGroup對象沒有處理異常的特殊要求,它可以將調用轉發到默認的未捕獲的異常處理程序。

因此我們可以自定一個類去實現該接口,該類主要用於收集錯誤信息和保存錯誤信息.

實現代碼

自己先寫一個錯誤的、會導致崩潰的代碼:

public class MainActivity extends Activity {  
  
    private String s;  
      
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        System.out.println(s.equals("any string"));  
    }  
}  

我們在這裏故意製造了一個潛在的運行期異常,當我們運行程序時就會出現以下界面:
異常描述
遇到軟件沒有捕獲的異常之後,系統會彈出這個默認的強制關閉對話框。

我們當然不希望用戶看到這種現象,簡直是對用戶心靈上的打擊,而且對我們的bug的修復也是毫無幫助的。我們需要的是軟件有一個全局的異常捕獲器,當出現一個我們沒有發現的異常時,捕獲這個異常,並且將異常信息記錄下來,上傳到服務器公開發這分析出現異常的具體原因。

剛纔在項目的結構圖中看到的CrashHandler.java實現了Thread.UncaughtExceptionHandler,使我們用來處理未捕獲異常的主要成員,代碼如下:

public class CrashHandler implements UncaughtExceptionHandler {  
      
    public static final String TAG = "CrashHandler";  
      
    //系統默認的UncaughtException處理類   
    private Thread.UncaughtExceptionHandler mDefaultHandler;  
    //CrashHandler實例  
    private static CrashHandler INSTANCE = new CrashHandler();  
    //程序的Context對象  
    private Context mContext;  
    //用來存儲設備信息和異常信息  
    private Map<String, String> infos = new HashMap<String, String>();  
  
    //用於格式化日期,作爲日誌文件名的一部分  
    private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");  
  
    /** 保證只有一個CrashHandler實例 */  
    private CrashHandler() {  
    }  
  
    /** 獲取CrashHandler實例 ,單例模式 */  
    public static CrashHandler getInstance() {  
        return INSTANCE;  
    }  
  
    /** 
     * 初始化 
     *  
     * @param context 
     */  
    public void init(Context context) {  
        mContext = context;  
        //獲取系統默認的UncaughtException處理器  
        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();  
        //設置該CrashHandler爲程序的默認處理器  
        Thread.setDefaultUncaughtExceptionHandler(this);  
    }  
  
    /** 
     * 當UncaughtException發生時會轉入該函數來處理 
     */  
    @Override  
    public void uncaughtException(Thread thread, Throwable ex) {  
        if (!handleException(ex) && mDefaultHandler != null) {  
            //如果用戶沒有處理則讓系統默認的異常處理器來處理  
            mDefaultHandler.uncaughtException(thread, ex);  
        } else {  
            try {  
                Thread.sleep(3000);  
            } catch (InterruptedException e) {  
                Log.e(TAG, "error : ", e);  
            }  
            //退出程序  
            android.os.Process.killProcess(android.os.Process.myPid());  
            System.exit(1);  
        }  
    }  
  
    /** 
     * 自定義錯誤處理,收集錯誤信息 發送錯誤報告等操作均在此完成. 
     *  
     * @param ex 
     * @return true:如果處理了該異常信息;否則返回false. 
     */  
    private boolean handleException(Throwable ex) {  
        if (ex == null) {  
            return false;  
        }  
        //使用Toast來顯示異常信息  
        new Thread() {  
            @Override  
            public void run() {  
                Looper.prepare();  
                Toast.makeText(mContext, "很抱歉,程序出現異常,即將退出.", Toast.LENGTH_LONG).show();  
                Looper.loop();  
            }  
        }.start();  
        //收集設備參數信息   
        collectDeviceInfo(mContext);  
        //保存日誌文件   
        saveCrashInfo2File(ex);  
        return true;  
    }  
      
    /** 
     * 收集設備參數信息 
     * @param ctx 
     */  
    public void collectDeviceInfo(Context ctx) {  
        try {  
            PackageManager pm = ctx.getPackageManager();  
            PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(), PackageManager.GET_ACTIVITIES);  
            if (pi != null) {  
                String versionName = pi.versionName == null ? "null" : pi.versionName;  
                String versionCode = pi.versionCode + "";  
                infos.put("versionName", versionName);  
                infos.put("versionCode", versionCode);  
            }  
        } catch (NameNotFoundException e) {  
            Log.e(TAG, "an error occured when collect package info", e);  
        }  
        Field[] fields = Build.class.getDeclaredFields();  
        for (Field field : fields) {  
            try {  
                field.setAccessible(true);  
                infos.put(field.getName(), field.get(null).toString());  
                Log.d(TAG, field.getName() + " : " + field.get(null));  
            } catch (Exception e) {  
                Log.e(TAG, "an error occured when collect crash info", e);  
            }  
        }  
    }  
  
    /** 
     * 保存錯誤信息到文件中 
     *  
     * @param ex 
     * @return  返回文件名稱,便於將文件傳送到服務器 
     */  
    private String saveCrashInfo2File(Throwable ex) {  
          
        StringBuffer sb = new StringBuffer();  
        for (Map.Entry<String, String> entry : infos.entrySet()) {  
            String key = entry.getKey();  
            String value = entry.getValue();  
            sb.append(key + "=" + value + "\n");  
        }  
          
        Writer writer = new StringWriter();  
        PrintWriter printWriter = new PrintWriter(writer);  
        ex.printStackTrace(printWriter);  
        Throwable cause = ex.getCause();  
        while (cause != null) {  
            cause.printStackTrace(printWriter);  
            cause = cause.getCause();  
        }  
        printWriter.close();  
        String result = writer.toString();  
        sb.append(result);  
        try {  
            long timestamp = System.currentTimeMillis();  
            String time = formatter.format(new Date());  
            String fileName = "crash-" + time + "-" + timestamp + ".log";  
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {  
                String path = "/sdcard/crash/";  
                File dir = new File(path);  
                if (!dir.exists()) {  
                    dir.mkdirs();  
                }  
                FileOutputStream fos = new FileOutputStream(path + fileName);  
                fos.write(sb.toString().getBytes());  
                fos.close();  
            }  
            return fileName;  
        } catch (Exception e) {  
            Log.e(TAG, "an error occured while writing file...", e);  
        }  
        return null;  
    }  
}  

在收集異常信息時,也可以使用Properties,因爲Properties有一個很便捷的方法properties.store(OutputStream out, String comments),用來將Properties實例中的鍵值對外輸到輸出流中,但是在使用的過程中發現生成的文件中異常信息打印在同一行,看起來極爲費勁,所以換成Map來存放這些信息,然後生成文件時稍加了些操作。

完成這個CrashHandler後,我們需要在一個Application環境中讓其運行,爲此,我們繼承android.app.Application,添加自己的代碼,CrashApplication.java代碼如下:

public class CrashApplication extends Application {  
    @Override  
    public void onCreate() {  
        super.onCreate();  
        CrashHandler crashHandler = CrashHandler.getInstance();  
        crashHandler.init(getApplicationContext());  
    }  
}  

}
最後,爲了讓我們的CrashApplication取代android.app.Application的地位,在我們的代碼中生效,我們需要修改AndroidManifest.xml:

<application android:name=".CrashApplication" ...>  
</application>

因爲我們上面的CrashHandler中,遇到異常後要保存設備參數和具體異常信息到SDCARD,所以我們需要在AndroidManifest.xml中加入讀寫SDCARD權限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  

搞定了上邊的步驟之後,我們來運行一下這個項目:

程序異常,即將退出

會發現,在我們的sdcard中生成了一個Log文件

用文本編輯器打開日誌文件,看一段日誌信息:

Caused by: java.lang.NullPointerException  
    at com.scott.crash.MainActivity.onCreate(MainActivity.java:13)  
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)  
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)  
    ... 11 more 

這些信息對於開發者來說幫助極大,所以我們需要將此日誌文件上傳到服務器。
這樣就達到了線上如果用戶出問題,就可以抓到對應的log問題了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章