成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Android應用源碼之捕獲全局異常

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

源碼簡介

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

源碼片段:

  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.     // 用來存儲設備信息和異常信息  
  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.      * 同步方法,以免單例多線程環境下出現異常 
  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.      * 初始化,把當前對象設置成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  返回文件名稱,便于將文件傳送到服務器 
  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 來源: 網絡整理
相關推薦

2025-02-17 00:25:00

Winform開發

2015-02-27 16:35:13

智能農業Android界面

2013-05-14 11:13:40

動態捕獲PythonPython異常

2017-03-21 16:34:38

iOS捕獲異常

2021-03-13 17:38:51

Python警告開發

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

網易布局

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倉庫管理系統SQLight

2023-12-27 07:53:08

全局異常處理處理應用

2014-08-15 13:24:32

Android之SQL

2022-03-04 08:31:07

Spring異常處理

2013-05-21 14:22:29

Android游戲開發捕獲屏幕雙擊事件

2024-11-11 11:21:30

虛擬機Python跳轉表
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 成人亚洲精品久久久久软件 | 亚洲第一天堂 | 日韩欧美一级片 | 日韩国产欧美一区 | 国产精品无码专区在线观看 | 日韩成人影院 | 欧美一区免费 | 日本久久网 | 欧美精品在线观看 | 欧美久久久久久久久 | 亚洲精品视频一区二区三区 | 中文字幕亚洲专区 | 在线观看黄视频 | 91精品亚洲 | 日韩欧美国产精品一区二区 | 欧美男人天堂 | 久久av一区 | 国产aⅴ| 视频国产一区 | 久久久精品网 | 中文字幕亚洲视频 | 国产乱码精品1区2区3区 | 国产成人高清视频 | 超碰美女在线 | 午夜日韩 | 人人操日日干 | 久久高清国产视频 | 国产精品久久久久国产a级 欧美日本韩国一区二区 | 亚洲国产中文在线 | 99久久国产综合精品麻豆 | 久久里面有精品 | 欧美精品在欧美一区二区 | 欧美a级成人淫片免费看 | 视频在线观看一区二区 | 国产精品永久免费视频 | 久久69精品久久久久久久电影好 | 亚洲日韩中文字幕一区 | 亚洲男人天堂2024 | 午夜影院中文字幕 | 国产一区二区三区在线视频 | 一色桃子av一区二区 |