博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android性能优化之UncaughtExceptionHandler定制自己的错误日志系统
阅读量:5838 次
发布时间:2019-06-18

本文共 3343 字,大约阅读时间需要 11 分钟。

前言:

每当我们app测试的时候,测试人员总是对我们说这里崩溃了,那里挂掉了!我们只能默默接受,然后尝试着重现bug,更可悲的是有时候bug很难复现,为了解决这种现状所以我们要尝试这建立一个自己的bug日志系统。

实现原理:

Java为我们提供了一个机制,用来捕获并处理在一个线程对象中抛出的未检测异常,以避免程序终止。我们可以通过UncaughtExceptionHandler来实现这种机制。

 

具体实现:

public class CrashManager implements UncaughtExceptionHandler {    public static final String TAG = "CrashHandler";    // CrashHandler实例    private static CrashManager instance;    // 程序的Context对象    private Application application;    // 系统默认的UncaughtException处理类    private UncaughtExceptionHandler mDefaultHandler;    /**     * 保证只有一个CrashHandler实例     */    private CrashManager(Context context) {        application = (Application) context.getApplicationContext();        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();        Thread.setDefaultUncaughtExceptionHandler(this);    }    /**     * 获取CrashHandler实例 ,单例模式     */    public static CrashManager getInstance(Context context) {        CrashManager inst = instance;        if (inst == null) {            synchronized (CrashManager.class) {                inst = instance;                if (inst == null) {                    inst = new CrashManager(context.getApplicationContext());                    instance = inst;                }            }        }        return inst;    }    /**     * 当UncaughtException发生时会转入该函数来处理     */    @Override    public void uncaughtException(Thread thread, Throwable ex) {        TaskManager.getInstance(application).saveErrorLog(ex);        mDefaultHandler.uncaughtException(thread, ex);    }}

日志写入sdcard代码:

public class SaveErrorTask
extends Task
{ private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.CHINA); private Context context; private Throwable ex; public SaveErrorTask(Context context, Throwable ex) { setPriority(TaskPriority.UI_LOW); this.context = context; this.ex = ex; } @Override protected Void doInBackground(Object... arg0) { 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(); String time = formatter.format(new Date()); String fileName = time + ".txt"; StringBuilder stringBuffer = new StringBuilder(); DeviceInfo deviceInfo = Utils.getDeviceInfo(context); stringBuffer.append("\nsdkVersion:" + deviceInfo.sdkVersion); stringBuffer.append("\nmanufacturer:" + deviceInfo.manufacturer); stringBuffer.append("\nmodel:" + deviceInfo.model); stringBuffer.append("\nversion" + ConfigManager.getVersionName(context)); stringBuffer.append("\nerrorStr:" + result); stringBuffer.append("\ntime:" + time); String filePath = CacheFileUtils.getLogPath(fileName); CacheFileUtils.saveErrorStr(filePath, stringBuffer.toString()); return null; }}

初始化:

public class MyApplication extends MultiDexApplication {    @Override    public void onCreate() {        super.onCreate();        //初始化 错误日子系统        CrashManager.getInstance(this);           }}

 

 

展望:我们也可以把日志再下次启动的时候发送至我们自己的日志服务器,监控用户错误信息

转载地址:http://ksjcx.baihongyu.com/

你可能感兴趣的文章
灰度图像和彩色图像
查看>>
argparse - 命令行选项与参数解析(转)
查看>>
修改上一篇文章的node.js代码,支持默认页及支持中文
查看>>
java只能的round,ceil,floor方法的使用
查看>>
新开的博客,为自己祝贺一下
查看>>
采用JXL包进行EXCEL数据写入操作
查看>>
将txt文件转化为json进行操作
查看>>
我的2014-相对奢侈的生活
查看>>
Java设计模式
查看>>
华为OJ 名字美丽度
查看>>
微信公众号与APP微信第三方登录账号打通
查看>>
mysql-This version of MySQL doesn’t yet support ‘LIMIT & IN/ALL/ANY/SOME 错误解决
查看>>
基本概念复习
查看>>
重构第10天:提取方法(Extract Method)
查看>>
通过Roslyn构建自己的C#脚本(更新版)(转)
查看>>
红黑树
查看>>
【数据库】
查看>>
WindowManager.LayoutParams 详解
查看>>
Android的Aidl安装方法
查看>>
Linux中rc的含义
查看>>