詳解Android全局異常的捕獲處理
在Android開發(fā)中在所難免的會出現(xiàn)程序crash,俗稱崩潰。用戶的隨意性訪問出現(xiàn)測試時未知的Bug導(dǎo)致我們的程序crash,此時我們是無法直接獲取的錯誤log的,也就無法修復(fù)Bug。這就會極大的影響用戶體驗(yàn),此時我們需要注冊一個功能來捕獲全局的異常信息,當(dāng)程序出現(xiàn)crash信息,我們把錯誤log記錄下來,上傳到服務(wù)器,以便于我們能及時修復(fù)bug。實(shí)現(xiàn)這個功能我們需要依賴于UncaughtExceptionHandler這個類,UncaughtExceptionHandler是一個接口,在Thread中。里面只有一個方法uncaughtException。當(dāng)我們注冊一個UncaughtExceptionHandler之后,當(dāng)我們的程序crash時就會回調(diào)uncaughtException方法,而uncaughtException方法帶有兩個參數(shù),參數(shù)中就存放這crash信息。接下來只看寫代碼
package hi.xiaoyu.crashhandler; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.lang.Thread.UncaughtExceptionHandler; import java.util.Date; import android.content.Context; import android.os.Environment; import android.util.Log; public class CrashHandler implements UncaughtExceptionHandler { private static CrashHandler instance; public static CrashHandler getInstance() { if (instance == null) { instance = new CrashHandler(); } return instance; } public void init(Context ctx) { Thread.setDefaultUncaughtExceptionHandler(this); } /** * 核心方法,當(dāng)程序crash 會回調(diào)此方法, Throwable中存放這錯誤日志 */ @Override public void uncaughtException(Thread arg0, Throwable arg1) { String logPath; if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { logPath = Environment.getExternalStorageDirectory() .getAbsolutePath() + File.separator + File.separator + "log"; File file = new File(logPath); if (!file.exists()) { file.mkdirs(); } try { FileWriter fw = new FileWriter(logPath + File.separator + "errorlog.log", true); fw.write(new Date() + "\n"); // 錯誤信息 // 這里還可以加上當(dāng)前的系統(tǒng)版本,機(jī)型型號 等等信息 StackTraceElement[] stackTrace = arg1.getStackTrace(); fw.write(arg1.getMessage() + "\n"); for (int i = 0; i < stackTrace.length; i++) { fw.write("file:" + stackTrace[i].getFileName() + " class:" + stackTrace[i].getClassName() + " method:" + stackTrace[i].getMethodName() + " line:" + stackTrace[i].getLineNumber() + "\n"); } fw.write("\n"); fw.close(); // 上傳錯誤信息到服務(wù)器 // uploadToServer(); } catch (IOException e) { Log.e("crash handler", "load file failed...", e.getCause()); } } arg1.printStackTrace(); android.os.Process.killProcess(android.os.Process.myPid()); } }
在Activity或者Application中注冊一下即可
CrashHandler crashHandler = CrashHandler.getInstance(); crashHandler.init(getApplicationContext());
這樣就實(shí)現(xiàn)了Android全局異常的捕獲處理,實(shí)現(xiàn)過程也比較簡單,希望對大家學(xué)習(xí)Android軟件編程有所幫助。
相關(guān)文章
使用RecyclerView實(shí)現(xiàn)點(diǎn)贊頭像疊加效果
這篇文章主要為大家詳細(xì)介紹了使用RecyclerView實(shí)現(xiàn)點(diǎn)贊頭像疊加效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-08-08Android中ExpandableListView的用法實(shí)例
這篇文章主要介紹了Android中ExpandableListView的用法,以實(shí)例形式展示了Android中的下拉list控件的用法,需要的朋友可以參考下2014-10-10Android開發(fā)之利用jsoup解析HTML頁面的方法
這篇文章主要介紹了Android開發(fā)之利用jsoup解析HTML頁面的方法,結(jié)合實(shí)例形式分析了Android基于jsoup jar包來抓取html頁面的實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-03-03Android?startActivityForResult的調(diào)用與封裝詳解
startActivityForResult?可以說是我們常用的一種操作了,目前有哪些方式實(shí)現(xiàn)?startActivityForResult?的功能呢?本文就來和大家詳細(xì)聊聊2023-03-03Android中ACTION_CANCEL的觸發(fā)機(jī)制與滑出子view的情況
這篇文章主要介紹了Android中ACTION_CANCEL的觸發(fā)機(jī)制與滑出子view的情況,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09Android實(shí)用圖文教程之代碼混淆、第三方平臺加固加密、渠道分發(fā)
這篇文章主要介紹了Android實(shí)用圖文教程之代碼混淆、第三方平臺加固加密、渠道分發(fā),需要的朋友可以參考下2014-12-12