android 全局crash

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/androidwubo/article/details/81451576
  1. 创建类
    ​​​

    public class CrashHandler implements Thread.UncaughtExceptionHandler {
        private static final String TAG = "CrashHandler";
        private Context mContext;
        private static CrashHandler mInstance;
        private Thread.UncaughtExceptionHandler mDefaultHandler;
        // 用来存储设备信息和异常信息
        private Map<String, String> mInfo = new HashMap<>();
        private DateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
        private CrashHandler() {}
    
        public static CrashHandler getInstance() {
            if (mInstance == null) {
                synchronized (CrashHandler.class) {
                    if (mInstance == null) {
                        mInstance = new CrashHandler();
                    }
                }
            }
            return mInstance;
        }
    
        public void init(Context context) {
            mContext = context;
            // 获取系统默认的UncaughtException处理器
            mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
            //设置该CrashHandler为程序的默认处理器
            Thread.setDefaultUncaughtExceptionHandler(this);
        }
    
        @Override
        public void uncaughtException(Thread t, Throwable e) {
            // 如果用户没有处理则让系统默认的异常处理器来处理
            if (!handleException(e) && mDefaultHandler != null) {
                mDefaultHandler.uncaughtException(t, e);
            } else {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e1) {
                    Log.e(TAG, "error", e);
                }
                // 退出程序
                Process.killProcess(Process.myPid());
                System.exit(1);
            }
        }
    
        // 自定义错误处理,收集错误信息,发送错误报告等操作均在此完成.
        private boolean handleException(Throwable e) {
            if (e == null) {
                return false;
            }
            // 使用Toast来显示异常信息
            new Thread() {
                @Override
                public void run() {
                    Looper.prepare();
                    Toast.makeText(mContext, "很抱歉,程序出现异常,即将退出.", Toast.LENGTH_SHORT).show();
                    Looper.loop();
                }
            }.start();
    
            //收集设备参数信息
            collectErrorInfo();
            //保存日志文件
            saveErrorInfo(e);
            return true;
        }
    
        // 收集设备参数信息
        private void collectErrorInfo() {
            PackageManager pm = mContext.getPackageManager();
            try {
                PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);
                if (pi != null) {
                    String versionName = TextUtils.isEmpty(pi.versionName) ? "未设置版本号" : pi.versionName;
                    String versionCode = pi.versionCode + "";
                    mInfo.put("versionName", versionName);
                    mInfo.put("versionCode", versionCode);
                }
    
                Field[] fields = Build.class.getFields();
                if (fields != null && fields.length > 0) {
                    for (Field field : fields) {
                        field.setAccessible(true);
                        try {
                            mInfo.put(field.getName(), field.get(null).toString());
                        } catch (IllegalAccessException e) {
                            Log.e(TAG, "an error occured when collect crash info", e);
                        }
                    }
                }
            } catch (PackageManager.NameNotFoundException e) {
                Log.e(TAG, "an error occured when collect package info", e);
            }
        }
    
        // 保存错误信息到文件中
        private void saveErrorInfo(Throwable e) {
            StringBuffer stringBuffer = new StringBuffer();
            for (Map.Entry<String, String> entry : mInfo.entrySet()) {
                String keyName = entry.getKey();
                String value = entry.getKey();
                stringBuffer.append(keyName+"="+value+"\n");
            }
    
            Writer writer = new StringWriter();
            PrintWriter printWriter = new PrintWriter(writer);
            e.printStackTrace(printWriter);
            Throwable cause = e.getCause();
            while (cause != null) {
                cause.printStackTrace(printWriter);
                cause = cause.getCause();
            }
    
            printWriter.close();
    
            String result = writer.toString();
            stringBuffer.append(result);
    
            long currentTime = System.currentTimeMillis();
            String time = mDateFormat.format(new Date());
            String fileName = "crash-" + time + "-" + currentTime + ".log";
    
            // 判断有没有SD卡
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/shanghaicrash");
                if (!dir.exists()) {
                    dir.mkdirs();
                }
    
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(dir + "/" + fileName);
                    fos.write(stringBuffer.toString().getBytes());
                } catch (FileNotFoundException e1) {
                    Log.e(TAG, "an error occured due to file not found", e);
                } catch (IOException e2) {
                    Log.e(TAG, "an error occured while writing file...", e);
                } finally {
                    try {
                        fos.close();
                    } catch (IOException e1) {
                        Log.e(TAG, "an error occured when close file", e);
                    }
                }
            }
        }
    }
  2. 在application中初始化

    CrashHandler.getInstance().init(this);
  3. 在设定的位置查看log

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页