安卓APp捕获异常
来源:互联网 发布:mac os 10.10 u盘安装 编辑:程序博客网 时间:2024/05/01 03:15
直接贴代码了
package adadd;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.os.Environment;import android.os.Looper;import android.util.Log;import java.io.File;import java.io.FileOutputStream;import java.io.PrintWriter;import java.io.StringWriter;import java.io.Writer;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.Map;/** * 异常捕获 * * @author huting * @date 2016/1/15 */public class MyExceptionHandler implements Thread.UncaughtExceptionHandler{ private final static String TAG = "UncaughtException"; private static MyExceptionHandler mUncaughtException; private Context context; private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); private Map<String, String> infos = new HashMap<String, String>(); public Context getContext() { return context; } public void setContext(Context context) { this.context = context; } private MyExceptionHandler() {} public synchronized static MyExceptionHandler getInstance() { if (mUncaughtException == null) { mUncaughtException = new MyExceptionHandler(); } return mUncaughtException; } public void init() { Thread.setDefaultUncaughtExceptionHandler(mUncaughtException); } @Override public void uncaughtException(Thread thread, Throwable throwable) { saveCrashInfo2File(throwable); Log.e(TAG, "uncaughtException thread : " + thread + "||name=" + thread.getName() + "||id=" + thread.getId() + "||exception=" + throwable); showDialog(); } private void showDialog() { new Thread() { @Override public void run() { Looper.prepare(); new AlertDialog.Builder(context).setTitle("泪奔提示").setCancelable(false).setMessage("程序出现异常...") .setNeutralButton("我知道了", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //System.exit(0); // ((Activity)context).finish(); dialog.dismiss(); System.exit(0); } }).create().show(); Looper.loop(); } }.start(); } 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/ShopNC/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; }}
注册在application
MyExceptionHandler mUncaughtException = MyExceptionHandler.getInstance();mUncaughtException.init();
调用在activityMyExceptionHandler.getInstance().setContext(this);就是这么简单
0 0
- 安卓APp捕获异常
- 安卓开发 捕获异常并重启app
- 安卓全局异常捕获
- 安卓APP崩溃(crash)后重新启动,捕获全局异常重启APP
- 安卓捕获运行时异常
- APP的异常捕获
- android app异常捕获
- 安卓全局异常捕获--自定义错误详情页
- 安卓捕获RuntimeException,ANR,Native信号异常
- 安卓实现分业务模块异常捕获,全局异常不崩溃,应用继续运行!
- 捕获android app应用的全部异常
- 捕获IOS APP中出现的异常
- 捕获IOS APP中出现的异常
- Android 全局异常捕获 重启app
- 安卓开发-app异常退出的善后工作->UncaughtExceptionHandler
- 安卓捕获应用的运行时异常并保存代码
- 安卓如何捕获奔溃异常日志及上传到服务端
- 安卓开发定义一个全局的捕获异常,提升客户体验
- java 远程调试
- Android中使用ALSA声卡及alsa.conf asound.conf
- ROS Learning-027 (提高篇-005 A Mobile Base-03) 控制移动平台 --- Twist 消息
- NEFU OJ16 Function Run Fun
- Pyqt5系列(五)-基本界面组件之inputDialog
- 安卓APp捕获异常
- HDU 3518 Boring counting 的C++解法
- tjut 3579
- java一维数组的学习
- 跟着angularjs2官方文档学习(五)
- 支持向量机
- Redis 跨域登录 ------- 待更~~~!!!
- 哈夫曼编码(Add All,uva 10954)
- C#与单片机通信中遇到的数据类型转换