• 【转载】通用 application 彻底退出应用 获崩溃异常,保存错误日志,并重启应用


    转载地址:http://zheyiw.iteye.com/blog/1670990

    个人笔记:
    通用 application
    1、收集所有 avtivity 用于彻底退出应用
    2、捕获崩溃异常,保存错误日志,并重启应用
    
    
    public class HKBaseApplication extends Application {
        // activity对象列表,用于activity统一管理
        private List<Activity> activityList;
        // 异常捕获
        protected boolean isNeedCaughtExeption = true;// 是否捕获未知异常
        private PendingIntent restartIntent;
        private MyUncaughtExceptionHandler uncaughtExceptionHandler;
        private String packgeName;
    
        @Override
        public void onCreate() {
            super.onCreate();
    
            activityList = new ArrayList<Activity>();
            packgeName = getPackageName();
    
            if (isNeedCaughtExeption) {
                cauchException();
            }
        }
    
        // -------------------异常捕获-----捕获异常后重启系统-----------------//
    
        private void cauchException() {
            Intent intent = new Intent();
            // 参数1:包名,参数2:程序入口的activity
            intent.setClassName(packgeName, packgeName + ".LoginActivity");
            restartIntent = PendingIntent.getActivity(getApplicationContext(), -1, intent,
                    Intent.FLAG_ACTIVITY_NEW_TASK);
    
            // 程序崩溃时触发线程
            uncaughtExceptionHandler = new MyUncaughtExceptionHandler();
            Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler);
        }
    
        // 创建服务用于捕获崩溃异常
        private class MyUncaughtExceptionHandler implements UncaughtExceptionHandler {
            @Override
            public void uncaughtException(Thread thread, Throwable ex) {
                // 保存错误日志
                saveCatchInfo2File(ex);
    
                // 1秒钟后重启应用
                AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, restartIntent);
    
                // 关闭当前应用
                finishAllActivity();
                finishProgram();
            }
        };
    
        /**
         * 保存错误信息到文件中
         * 
         * @return 返回文件名称
         */
        private String saveCatchInfo2File(Throwable ex) {
            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 sb = writer.toString();
            try {
                DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
                String time = formatter.format(new Date());
                String fileName = time + ".txt";
                System.out.println("fileName:" + fileName);
                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                    String filePath = Environment.getExternalStorageDirectory() + "/HKDownload/" + packgeName
                            + "/crash/";
                    File dir = new File(filePath);
                    if (!dir.exists()) {
                        if (!dir.mkdirs()) {
                            // 创建目录失败: 一般是因为SD卡被拔出了
                            return "";
                        }
                    }
                    System.out.println("filePath + fileName:" + filePath + fileName);
                    FileOutputStream fos = new FileOutputStream(filePath + fileName);
                    fos.write(sb.getBytes());
                    fos.close();
                    //文件保存完了之后,在应用下次启动的时候去检查错误日志,发现新的错误日志,就发送给开发者
                }
                return fileName;
            } catch (Exception e) {
                System.out.println("an error occured while writing file..." + e.getMessage());
            }
            return null;
        }
    
        // ------------------------------activity管理-----------------------//
    
        // activity管理:从列表中移除activity
        public void removeActivity(Activity activity) {
            activityList.remove(activity);
        }
    
        // activity管理:添加activity到列表
        public void addActivity(Activity activity) {
            activityList.add(activity);
        }
    
        // activity管理:结束所有activity
        public void finishAllActivity() {
            for (Activity activity : activityList) {
                if (null != activity) {
                    activity.finish();
                }
            }
        }
    
        // 结束线程,一般与finishAllActivity()一起使用
        // 例如: finishAllActivity;finishProgram();
        public void finishProgram() {
            android.os.Process.killProcess(android.os.Process.myPid());
        }
    }
  • 相关阅读:
    用JavaScript实现的将GB2312转换为UTF8编码
    一行内文本超出指定宽度溢出的处理
    JavaScript delete
    深入学习Enterprise Library for .NET Framework 2.0的Cache机制——分析篇
    CSS兼容技巧
    IE6,IE7,FF兼容CSS的差别及解决办法
    JavaScript获取当前文件全路径、当前目录、当前文件名
    DIV+CSS相对IE6 IE7和IE8兼容问题整理
    ie8兼容ie7css的方法和minheight的兼容问题
    Atitit.得到网络邻居列表java php c#.net python
  • 原文地址:https://www.cnblogs.com/lihualuo/p/4184563.html
Copyright © 2020-2023  润新知