Android捕獲全域性異常資訊並實現上傳

2021-08-26 11:08:51 字數 1378 閱讀 8604

在做專案時,經常會把錯誤利用異常丟擲去,這樣在開發時就可以通過手機丟擲的異常排查錯誤。但是當程式開發完畢,版本穩定,需要上線時,為了避免丟擲異常影響使用者感受,可以用uncaughtexceptionhandler捕獲全域性異常,對異常做出處理。比如我們可以獲取到丟擲異常的時間、手機的硬體資訊、錯誤的堆疊資訊,然後將獲取到的所有的資訊傳送到伺服器中,也可以傳送到指定的郵件中,以便及時修改bug。

示例:自定義異常類實現uncaughtexceptionhandler介面,當某個頁面出現異常就會呼叫uncaughtexception這個方法,我們可以在這個方法中獲取異常資訊、時間等,然後將獲取到的資訊傳送到我們指定的伺服器

/**

* 自定義的 異常處理類 , 實現了 uncaughtexceptionhandler介面

* @author administrator

* */

public class mycrashhandler implements uncaughtexceptionhandler

public static synchronized mycrashhandler getinstance()else

} public void init(context context,doubanservice service)

public void uncaughtexception(thread arg0, throwable arg1) catch (exception e)

//乾掉當前的程式

android.os.process.killprocess(android.os.process.mypid());

} /**

* 獲取錯誤的資訊

* @param arg1

* @return

*/private string geterrorinfo(throwable arg1)

/*** 獲取手機的硬體資訊

* @return

*/private string getmobileinfo()

} catch (exception e)

return sb.tostring();

} /**

* 獲取手機的版本資訊

* @return

*/private string getversioninfo() catch (exception e)

}}

/**

* @author administrator

* */

public noteentry entry;

@override

public void oncreate()

}

android 捕獲全域性異常

很久以前就想弄這個,那會整了,沒弄出來。總是卡死,彈不出dialog,現在再試試,發現問題了,小記.這個說了怎麼使用 這個解決了為什麼不能彈出視窗提示 小記 1 現在的dialog 使用的context 必須是乙個activity,所以弄個baseactivity吧。2 在handlerexcept...

全域性異常捕獲

全域性使用 類package com.bwei.heji.zlsj.view import android.content.context import android.os.looper import android.util.log import android.widget.toast pub...

android捕獲全域性異常,並對異常做出處理

在做專案時,經常會把錯誤利用異常丟擲去,這樣在開發時就可以通過手機拋的異常排查錯誤,很方便。但是當程式開發完畢,版本穩定,需要上線時,為了避免丟擲異常影響使用者感受,可以捕獲全域性異常,對異常做出處理。具體的實方法如下 public class myuncaughtexceptionhandler ...