JavaShuo
欄目
標籤
Android application捕獲崩潰異常
時間 2019-11-06
標籤
android
application
捕獲
崩潰
異常
欄目
Android
简体版
原文
原文鏈接
Java代碼
我的筆記:
通用 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());
}
}
來源:
http://zheyiw.iteye.com/blog/1670990
來自爲知筆記(Wiz)
相關文章
1.
Flutter異常崩潰捕捉
2.
Android應用崩潰後異常捕獲並重啓
3.
Android 捕獲異常並在應用崩潰後重啓應用
4.
APP崩潰的異常捕捉
5.
Flutter異常捕獲和Crash崩潰日誌收集
6.
Android 應用崩潰捕獲工具 xCrash
7.
panic 捕獲及 throw 崩潰
8.
異常處理 - Native 層的崩潰捕獲機制及實現
9.
Android 異常捕獲
10.
WPF異常捕獲,並使程序不崩潰!
更多相關文章...
•
PHP 7 異常
-
PHP 7 新特性
•
C# 異常處理
-
C#教程
•
常用的分佈式事務解決方案
•
Kotlin學習(一)基本語法
相關標籤/搜索
崩潰
捕獲
application
異常
常見異常
Mybatis異常
Android
MyBatis教程
PHP 7 新特性
PHP教程
0
分享到微博
分享到微信
分享到QQ
每日一句
每一个你不满意的现在,都有一个你没有努力的曾经。
最新文章
1.
「插件」Runner更新Pro版,幫助設計師遠離996
2.
錯誤 707 Could not load file or assembly ‘Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKe
3.
Jenkins 2018 報告速覽,Kubernetes使用率躍升235%!
4.
TVI-Android技術篇之註解Annotation
5.
android studio啓動項目
6.
Android的ADIL
7.
Android卡頓的檢測及優化方法彙總(線下+線上)
8.
登錄註冊的業務邏輯流程梳理
9.
NDK(1)創建自己的C/C++文件
10.
小菜的系統框架界面設計-你的評估是我的決策
本站公眾號
歡迎關注本站公眾號,獲取更多信息
相關文章
1.
Flutter異常崩潰捕捉
2.
Android應用崩潰後異常捕獲並重啓
3.
Android 捕獲異常並在應用崩潰後重啓應用
4.
APP崩潰的異常捕捉
5.
Flutter異常捕獲和Crash崩潰日誌收集
6.
Android 應用崩潰捕獲工具 xCrash
7.
panic 捕獲及 throw 崩潰
8.
異常處理 - Native 層的崩潰捕獲機制及實現
9.
Android 異常捕獲
10.
WPF異常捕獲,並使程序不崩潰!
>>更多相關文章<<