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 来源: 网络整理
相关推荐

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桌面插件

2015-03-30 14:24:06

网易布局

2021-09-26 09:40:25

React代码前端

2010-02-26 10:14:25

WCF全局错误捕获

2013-09-13 13:15:28

AndroidWebViewJavaScript

2022-08-16 10:44:11

Sentry前端异常

2015-03-31 18:26:43

陌陌社交

2015-03-23 17:52:05

Android仓库管理系统SQLight

2023-12-27 07:53:08

全局异常处理处理应用

2013-05-21 14:22:29

Android游戏开发捕获屏幕双击事件

2022-03-04 08:31:07

Spring异常处理

2014-08-15 13:24:32

Android之SQL

2014-12-31 15:05:11

Android流量监控

2023-08-10 13:46:48

前端资源优化
点赞
收藏

51CTO技术栈公众号