zoukankan      html  css  js  c++  java
  • UncaughtExceptionHandler

    /**
     * 崩溃信息处理
     * Created by travis on 2016/1/21.
     */
    public class VIIUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
        private static final String TAG = VIIUncaughtExceptionHandler.class.getSimpleName();
        private Thread.UncaughtExceptionHandler mDefaultUncaughtExceptionHandler;
    
    
        public VIIUncaughtExceptionHandler() {
            this.mDefaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
        }
    
        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
            final Writer writer = new StringWriter();
            final PrintWriter printWriter = new PrintWriter(writer);
    
            StackTraceElement[] trace = ex.getStackTrace();
            StackTraceElement[] tem = new StackTraceElement[trace.length + 3];
            System.arraycopy(trace, 0, tem, 0, trace.length);
            tem[trace.length] = new StackTraceElement("android", "MODEL", Build.MODEL, -1);
            tem[trace.length + 1] = new StackTraceElement("android", "VERSION", Build.VERSION.RELEASE, -1);
            tem[trace.length + 2] = new StackTraceElement("android", "FINGERPRINT", Build.FINGERPRINT, -1);
            ex.setStackTrace(trace);
            ex.printStackTrace(printWriter);
            String dung = writer.toString();
            printWriter.close();
            collectDung(dung);
    
            mDefaultUncaughtExceptionHandler.uncaughtException(thread, ex);
        }
    
        private void collectDung(String dung) {
            upload(dung);
            store(dung);
        }
    
        /**
         * 把错误信息上传到服务器
         *
         * @param dung
         */
        private void upload(String dung) {
    
        }
    
        /**
         * 把错误信息保存到本地
         */
        private void store(String dung) {
            if (FileUtils.isExternalStorageMounted()) {
                File root = new File(Environment.getExternalStorageDirectory() + AppConfig.root);
                if (!root.exists()) {
                    root.mkdirs();
                }
                File file = new File(root, AppConfig.log.substring(AppConfig.log.lastIndexOf("/") + 1));
                BufferedWriter writer = null;
                try {
                    writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
                    writer.write(TimeUtils.getCurrentTime());
                    writer.newLine();
                    writer.write(dung);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (writer != null) {
                        try {
                            writer.flush();
                            writer.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
    
  • 相关阅读:
    [Python Study Notes]进程信息(丁丁软件监控进程,http-post)
    [Python Study Notes]cpu信息
    [Python Study Notes]电池信息
    [Python Study Notes]内存信息
    [Python Study Notes]磁盘信息和IO性能
    [Python Study Notes]计算cpu使用率v0.1
    [Python Study Notes]计算cpu使用率
    [Python Study Notes]psutil模块
    [解决问题] E: 无法获得锁 /var/lib/dpkg/lock
    [Python Study Notes] python面试题总结
  • 原文地址:https://www.cnblogs.com/hsji/p/5148794.html
Copyright © 2011-2022 走看看