您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)Android CrashHandler如何異常捕獲,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
CrashHandler類
用于記錄crash原因保存到sd卡中。
public class CrashHandler implements Thread.UncaughtExceptionHandler { private static final String TAG = "CrashHandler"; private static final boolean DEBUG = true; //文件路徑 private static final String PATH = Environment.getExternalStorageDirectory().getPath() +File.separator+ "crash"; private static final String FILE_NAME = "crash"; private static final String FILE_NAME_SUFEIX = ".trace"; private static Thread.UncaughtExceptionHandler mDefaultCrashHandler; private static CrashHandler mCrashHandler = new CrashHandler(); private Context mContext; private CrashHandler() { } public static CrashHandler getInstance() { return mCrashHandler; } public void init(Context context) { mDefaultCrashHandler = Thread.getDefaultUncaughtExceptionHandler(); Thread.setDefaultUncaughtExceptionHandler(this); mContext = context.getApplicationContext(); } @Override public void uncaughtException(Thread thread, Throwable ex) { try { //將文件寫入sd卡 writeToSDcard(ex); //寫入后在這里可以進(jìn)行上傳操作 } catch (IOException e) { e.printStackTrace(); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } ex.printStackTrace(); //如果系統(tǒng)提供了默認(rèn)異常處理就交給系統(tǒng)進(jìn)行處理,否則自己進(jìn)行處理。 if (mDefaultCrashHandler != null) { mDefaultCrashHandler.uncaughtException(thread, ex); } else { Process.killProcess(Process.myPid()); } } //將異常寫入文件 private void writeToSDcard(Throwable ex) throws IOException, PackageManager.NameNotFoundException { //如果沒有SD卡,直接返回 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { return; } File filedir = new File(PATH); if (!filedir.exists()) { filedir.mkdirs(); } long currenttime = System.currentTimeMillis(); String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(currenttime)); File exfile = new File(PATH +File.separator+FILE_NAME+time + FILE_NAME_SUFEIX); PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(exfile))); Log.e("錯(cuò)誤日志文件路徑",""+exfile.getAbsolutePath()); pw.println(time); PackageManager pm = mContext.getPackageManager(); PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES); //當(dāng)前版本號 pw.println("App Version:" + pi.versionName + "_" + pi.versionCode); //當(dāng)前系統(tǒng) pw.println("OS version:" + Build.VERSION.RELEASE + "_" + Build.VERSION.SDK_INT); //制造商 pw.println("Vendor:" + Build.MANUFACTURER); //手機(jī)型號 pw.println("Model:" + Build.MODEL); //CPU架構(gòu) pw.println("CPU ABI:" + Build.CPU_ABI); ex.printStackTrace(pw); pw.close(); } }
調(diào)用
public class BaseApplication extends Application { @Override public void onCreate() { super.onCreate(); CrashHandler crashHandler=CrashHandler.getInstance(); crashHandler.init(this); } }
測試
mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { throw new RuntimeException("拋出一個(gè)異常"); } });
最后記得添加文件權(quán)限,并調(diào)用BaseApplication
關(guān)于“Android CrashHandler如何異常捕獲”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。