編輯:關於 Android 程式設計
在實際開發中,為了能在程式崩潰時收集到有用的錯誤資訊並上報,我們可以自訂一個全域的異常處理器。核心程式碼如下:
public class ErrorReport implements Thread.UncaughtExceptionHandler{ public static final String TAG = GouMinErrorReport; private Context mContext; // 系統默認的 UncaughtException 處理類 private Thread.UncaughtExceptionHandler mDefaultHandler; private String logStr; public ErrorReport(Context context) { mContext = context; // 獲取系統默認的 UncaughtException 處理器 mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler(); Thread.setDefaultUncaughtExceptionHandler(this); } @Override public void uncaughtException(Thread thread, Throwable ex) { GMLog.i(TAG, uncaughtException); GMLog.i(TAG, ex.getLocalizedMessage()); ex.printStackTrace(); logStr = getStackTrace(ex); // 使用 Toast 來顯示異常信息 new Thread() { @Override public void run() { Looper.prepare(); Toast.makeText(mContext, 很抱歉,程序出現異常,即將退出。, Toast.LENGTH_SHORT) .show(); sendReport(logStr); Looper.loop(); } }.start(); // 如果用戶沒有處理則讓系統默認的異常處理器來處理 if (mDefaultHandler != null) { mDefaultHandler.uncaughtException(thread, ex); } } private void sendReport(String stackTrace) { GMLog.i(TAG, sendReport); HttpClient httpclient = new DefaultHttpClient(); final HttpPost httppost = new HttpPost(mErrorReportReq.getUrl()); try { httppost.setEntity(new StringEntity(stackTrace) )); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } HttpResponse httpResponse; try { httpResponse = httpclient.execute(httppost); final int statusCode = httpResponse.getStatusLine().getStatusCode(); GMLog.i(TAG, sendReport response statusCode is: + statusCode); if (statusCode == HttpStatus.SC_OK) { String response = EntityUtils.toString( httpResponse.getEntity(), HTTP.UTF_8); GMLog.i(TAG, sendReport response: + response); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { GMLog.i(TAG, sendReport finally); if (httpclient != null) { httpclient.getConnectionManager().shutdown(); httpclient = null; } } } private String getStackTrace(Throwable th) { final Writer result = new StringWriter(); final PrintWriter printWriter = new PrintWriter(result); // If the exception was thrown in a background thread inside // AsyncTask, then the actual exception can be found with getCause Throwable cause = th; while (cause != null) { cause.printStackTrace(printWriter); cause = cause.getCause(); } final String stacktraceAsString = result.toString(); printWriter.close(); return stacktraceAsString; } }對於 Android 的異常退出,關鍵在於實作了 Thread.UncaughtExceptionHandler 這個介面。它的運作邏輯是:在構造時先保存系統預設的異常處理器,再把當前物件設為全域的預設處理器;一旦程式拋出未捕獲的異常,就會進入 uncaughtException 方法,此時我們把堆棧資訊提取出來、用 Toast 提示使用者,並透過 sendReport 把錯誤堆棧上報到伺服器,最後再交給系統預設處理器做善後。
接著,只需要在 Application 中註冊這個處理器,就能讓整個 App 都具備崩潰捕獲能力:
public class MyApplication extends Application { ErrorReport errorReport; @Override public void onCreate() { super.onCreate(); errorReport = new ErrorReport(this); } }這樣一來,無論是哪個執行緒拋出未捕獲異常,都能被統一捕獲、提示並上報,極大地方便了線上問題的排查與定位。
Android Xfermode 圖層混合:PorterDuff 18 種模式效果解析
在 Android 繪圖中,PorterDuffXfermode 是處理圖層混合的重要工具。它允許開發者指定兩個圖層合成時的規則,例如保留交集、覆蓋、變暗、變亮或透明化。
android:descendantFocusability 用法解析:解決 ListView item 點擊沒反應
這是一個開發中很常遇到的怪現象:自訂 ListView 的 item 裡放了 Button、ImageButton 或 CheckBox 這類子控件,結果點擊整列完全沒反
Android 自訂 View 實作輕量柱狀圖
為什麼要自訂柱狀圖專案中偶爾會需要柱狀圖來呈現數據。現成開源庫有時過於龐大,或接入方式不夠直覺。此時可考慮自訂 View,直接以 Canvas 繪製輕量圖表,並依需求調整
一個遊戲角色的例子,讓你徹底看懂裝飾者模式
為什麼不從定義講起 設計模式的定義往往抽象到難以理解,讀完還是不知道何時該用。這些模式的誕生,其實都是前輩在實作時撞到了同一堵牆,才總結出的解法。所以本篇換個方式:先讓你