自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android應(yīng)用源碼之捕獲全局異常

移動開發(fā)
本項目就是一個簡單的全局異常捕捉例子,捕捉到異常以后可以把異常信息寫入文件以供后來分析或者用友好的方式進行提示后再退出程序。

源碼簡介

本項目就是一個簡單的全局異常捕捉例子,捕捉到異常以后可以把異常信息寫入文件以供后來分析或者用友好的方式進行提示后再退出程序。
源碼運行截圖

源碼片段:

  1. public class UncaughtException implements UncaughtExceptionHandler { 
  2.     private final static String TAG = "UncaughtException"
  3.     private static UncaughtException mUncaughtException; 
  4.     private Context context; 
  5.     private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");  
  6.     // 用來存儲設(shè)備信息和異常信息  
  7.     private Map<string, string=""> infos = new HashMap<string, string="">();  
  8.     public Context getContext() { 
  9.         return context; 
  10.     } 
  11.   
  12.     public void setContext(Context context) { 
  13.         this.context = context; 
  14.     } 
  15.   
  16.     private UncaughtException() { 
  17.         // TODO Auto-generated constructor stub 
  18.     } 
  19.   
  20.     /** 
  21.      * 同步方法,以免單例多線程環(huán)境下出現(xiàn)異常 
  22.      * 
  23.      * @return 
  24.      */ 
  25.     public synchronized static UncaughtException getInstance() { 
  26.         if (mUncaughtException == null) { 
  27.             mUncaughtException = new UncaughtException(); 
  28.         } 
  29.         return mUncaughtException; 
  30.     } 
  31.   
  32.     /** 
  33.      * 初始化,把當前對象設(shè)置成UncaughtExceptionHandler處理器 
  34.      */ 
  35.     public void init() { 
  36.         Thread.setDefaultUncaughtExceptionHandler(mUncaughtException); 
  37.     } 
  38.   
  39.     @Override 
  40.     public void uncaughtException(Thread thread, Throwable ex) { 
  41.         // TODO Auto-generated method stub 
  42.         //處理異常,我們還可以把異常信息寫入文件,以供后來分析。 
  43.        saveCrashInfo2File(ex); 
  44.         Log.e(TAG, "uncaughtException thread : " + thread + "||name=" + thread.getName() + "||id=" + thread.getId() + "||exception=" + ex); 
  45.    /*   Looper.prepare(); 
  46.         Toast.makeText(context, "程序異常,立即退出", 1).show(); 
  47.       System.exit(0); 
  48.         Looper.loop();*/ 
  49.           
  50.          showDialog() ; 
  51.     } 
  52.   
  53.     private void showDialog() { 
  54.         new Thread() { 
  55.             @Override 
  56.             public void run() { 
  57.                 Looper.prepare(); 
  58.                 new AlertDialog.Builder(context).setTitle("淚奔提示").setCancelable(false).setMessage("大爺我崩潰了..."
  59.                         .setNeutralButton("我知道了"new OnClickListener() { 
  60.                             @Override 
  61.                             public void onClick(DialogInterface dialog, int which) { 
  62.                                 System.exit(0); 
  63.                                   
  64.                             } 
  65.                         }).create().show(); 
  66.                 Looper.loop(); 
  67.             } 
  68.         }.start(); 
  69.     } 
  70.       
  71.     /** 
  72.      * 保存錯誤信息到文件中 
  73.     * 
  74.      * @param ex 
  75.      * @return  返回文件名稱,便于將文件傳送到服務(wù)器 
  76.      */  
  77.     private String saveCrashInfo2File(Throwable ex) {  
  78.         StringBuffer sb = new StringBuffer(); 
  79.          
  80.         long timestamp = System.currentTimeMillis();  
  81.         String time = formatter.format(new Date()); 
  82.         sb.append("\n"+time+"----"); 
  83.         for (Map.Entry<string, string=""> entry : infos.entrySet()) {  
  84.             String key = entry.getKey();  
  85.             String value = entry.getValue();  
  86.             sb.append(key + "=" + value + "\n");  
  87.         }  
  88.     
  89.         Writer writer = new StringWriter();  
  90.         PrintWriter printWriter = new PrintWriter(writer);  
  91.         ex.printStackTrace(printWriter);  
  92.         Throwable cause = ex.getCause();  
  93.         while (cause != null) {  
  94.             cause.printStackTrace(printWriter);  
  95.             cause = cause.getCause();  
  96.         }  
  97.         printWriter.close();  
  98.     
  99.         String result = writer.toString();  
  100.         sb.append(result);  
  101.         try {  
  102.                
  103.             String fileName = "exception.log";  
  104.                 
  105.             if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {  
  106.                 String path = "/sdcard/crash/";  
  107.                 File dir = new File(path);  
  108.                 if (!dir.exists()) {  
  109.                     dir.mkdirs();  
  110.                 }  
  111.                 FileOutputStream fos = new FileOutputStream(path + fileName,true);  
  112.                 fos.write(sb.toString().getBytes());  
  113.                 fos.close();  
  114.             }  
  115.     
  116.             return fileName;  
  117.         } catch (Exception e) {  
  118.             Log.e(TAG, "an error occured while writing file...", e);  
  119.         }  
  120.     
  121.         return null;  
  122.     }  
  123. }</string,></string,></string,> 

源碼鏈接:http://down.51cto.com/data/1980812

責任編輯:chenqingxiang 來源: 網(wǎng)絡(luò)整理
相關(guān)推薦

2025-02-17 00:25:00

Winform開發(fā)

2015-02-27 16:35:13

智能農(nóng)業(yè)Android界面

2013-05-14 11:13:40

動態(tài)捕獲PythonPython異常

2017-03-21 16:34:38

iOS捕獲異常

2021-03-13 17:38:51

Python警告開發(fā)

2022-11-28 07:35:52

前端錯誤

2015-02-11 17:49:35

Android源碼自定義控件

2015-02-28 15:15:47

插件Android桌面插件

2021-09-26 09:40:25

React代碼前端

2015-03-30 14:24:06

網(wǎng)易布局

2022-08-16 10:44:11

Sentry前端異常

2015-03-31 18:26:43

陌陌社交

2013-09-13 13:15:28

AndroidWebViewJavaScript

2010-02-26 10:14:25

WCF全局錯誤捕獲

2015-03-23 17:52:05

Android倉庫管理系統(tǒng)SQLight

2023-12-27 07:53:08

全局異常處理處理應(yīng)用

2024-11-11 11:21:30

虛擬機Python跳轉(zhuǎn)表

2022-03-04 08:31:07

Spring異常處理

2013-05-21 14:22:29

Android游戲開發(fā)捕獲屏幕雙擊事件

2014-08-15 13:24:32

Android之SQL
點贊
收藏

51CTO技術(shù)棧公眾號