- 個人筆記:
- 通用 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());
- }
- }