Android應(yīng)用源碼之捕獲全局異常
作者:網(wǎng)絡(luò)整理
本項目就是一個簡單的全局異常捕捉例子,捕捉到異常以后可以把異常信息寫入文件以供后來分析或者用友好的方式進行提示后再退出程序。
源碼簡介
本項目就是一個簡單的全局異常捕捉例子,捕捉到異常以后可以把異常信息寫入文件以供后來分析或者用友好的方式進行提示后再退出程序。
源碼運行截圖
源碼片段:
- public class UncaughtException implements UncaughtExceptionHandler {
- private final static String TAG = "UncaughtException";
- private static UncaughtException mUncaughtException;
- private Context context;
- private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
- // 用來存儲設(shè)備信息和異常信息
- private Map<string, string=""> infos = new HashMap<string, string="">();
- public Context getContext() {
- return context;
- }
- public void setContext(Context context) {
- this.context = context;
- }
- private UncaughtException() {
- // TODO Auto-generated constructor stub
- }
- /**
- * 同步方法,以免單例多線程環(huán)境下出現(xiàn)異常
- *
- * @return
- */
- public synchronized static UncaughtException getInstance() {
- if (mUncaughtException == null) {
- mUncaughtException = new UncaughtException();
- }
- return mUncaughtException;
- }
- /**
- * 初始化,把當前對象設(shè)置成UncaughtExceptionHandler處理器
- */
- public void init() {
- Thread.setDefaultUncaughtExceptionHandler(mUncaughtException);
- }
- @Override
- public void uncaughtException(Thread thread, Throwable ex) {
- // TODO Auto-generated method stub
- //處理異常,我們還可以把異常信息寫入文件,以供后來分析。
- saveCrashInfo2File(ex);
- Log.e(TAG, "uncaughtException thread : " + thread + "||name=" + thread.getName() + "||id=" + thread.getId() + "||exception=" + ex);
- /* Looper.prepare();
- Toast.makeText(context, "程序異常,立即退出", 1).show();
- System.exit(0);
- Looper.loop();*/
- showDialog() ;
- }
- private void showDialog() {
- new Thread() {
- @Override
- public void run() {
- Looper.prepare();
- new AlertDialog.Builder(context).setTitle("淚奔提示").setCancelable(false).setMessage("大爺我崩潰了...")
- .setNeutralButton("我知道了", new OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- System.exit(0);
- }
- }).create().show();
- Looper.loop();
- }
- }.start();
- }
- /**
- * 保存錯誤信息到文件中
- *
- * @param ex
- * @return 返回文件名稱,便于將文件傳送到服務(wù)器
- */
- private String saveCrashInfo2File(Throwable ex) {
- StringBuffer sb = new StringBuffer();
- long timestamp = System.currentTimeMillis();
- String time = formatter.format(new Date());
- sb.append("\n"+time+"----");
- for (Map.Entry<string, string=""> entry : infos.entrySet()) {
- String key = entry.getKey();
- String value = entry.getValue();
- sb.append(key + "=" + value + "\n");
- }
- Writer writer = new StringWriter();
- PrintWriter printWriter = new PrintWriter(writer);
- ex.printStackTrace(printWriter);
- Throwable cause = ex.getCause();
- while (cause != null) {
- cause.printStackTrace(printWriter);
- cause = cause.getCause();
- }
- printWriter.close();
- String result = writer.toString();
- sb.append(result);
- try {
- String fileName = "exception.log";
- if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
- String path = "/sdcard/crash/";
- File dir = new File(path);
- if (!dir.exists()) {
- dir.mkdirs();
- }
- FileOutputStream fos = new FileOutputStream(path + fileName,true);
- fos.write(sb.toString().getBytes());
- fos.close();
- }
- return fileName;
- } catch (Exception e) {
- Log.e(TAG, "an error occured while writing file...", e);
- }
- return null;
- }
- }</string,></string,></string,>
源碼鏈接:http://down.51cto.com/data/1980812
責任編輯:chenqingxiang
來源:
網(wǎng)絡(luò)整理