android Activity崩潰日誌收集
每個android應用都是由一個Application和多個activity或者server構成.應用啟動時,會首先啟動Application.在Application的onCreate方法中呼叫
Thread.setDefaultUncaughtExceptionHandler(handler);
就可以捕獲導致應用崩潰的錯誤資訊了.
首先應用要有讀寫sd卡許可權
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
自定義一個Application,並在AndroidManifest.xml中使用這個Application
<application
android:name=".MyApplication">
...
</application>
public class MyApplication extends Application {
private static final String LOG_DIR = Environment
.getExternalStorageDirectory().getAbsolutePath() + "/oldfeel/log/";
private static final String LOG_NAME = getCurrentDateString() + ".txt";
private ArrayList<Activity> list = new ArrayList<Activity>();
@Override
public void onCreate() {
super.onCreate();
Thread.setDefaultUncaughtExceptionHandler(handler);
}
UncaughtExceptionHandler handler = new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
writeErrorLog(ex);
Intent intent = new Intent(getApplicationContext(),
CollapseActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
exit();
}
};
/**
* 列印錯誤日誌
*
* @param ex
*/
protected void writeErrorLog(Throwable ex) {
String info = null;
ByteArrayOutputStream baos = null;
PrintStream printStream = null;
try {
baos = new ByteArrayOutputStream();
printStream = new PrintStream(baos);
ex.printStackTrace(printStream);
byte[] data = baos.toByteArray();
info = new String(data);
data = null;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (printStream != null) {
printStream.close();
}
if (baos != null) {
baos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
Log.d("example", "崩潰資訊\n" + info);
File dir = new File(LOG_DIR);
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, LOG_NAME);
try {
FileOutputStream fileOutputStream = new FileOutputStream(file, true);
fileOutputStream.write(info.getBytes());
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 獲取當前日期
*
* @return
*/
private static String getCurrentDateString() {
String result = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",
Locale.getDefault());
Date nowDate = new Date();
result = sdf.format(nowDate);
return result;
}
/**
* Activity關閉時,刪除Activity列表中的Activity物件
*/
public void removeActivity(Activity a) {
list.remove(a);
}
/**
* 向Activity列表中新增Activity物件
*/
public void addActivity(Activity a) {
list.add(a);
}
/**
* 關閉Activity列表中的所有Activity
*/
public void exit() {
for (Activity activity : list) {
if (null != activity) {
activity.finish();
}
}
// 殺死該應用程式
android.os.Process.killProcess(android.os.Process.myPid());
}
}
系統錯誤後要還是要提示使用者系統錯誤.這個是崩潰activity,
<activity
android:name="com.example.test.CollapseActivity"
android:theme="@android:style/Theme.Holo.Dialog.MinWidth" >
</activity>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/collapse_restart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="重啟應用" />
<Button
android:id="@+id/collapse_exit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="退出應用" />
</LinearLayout>
public class CollapseActivity extends Activity {
private Button btnRestart, btnExit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.collapse_activity);
setTitle("應用崩潰了");
btnRestart = (Button) findViewById(R.id.collapse_restart);
btnExit = (Button) findViewById(R.id.collapse_exit);
btnRestart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
});
btnExit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
相關文章
- 簡便地Android崩潰日誌收集Android
- 【安卓筆記】崩潰日誌收集安卓筆記
- Flutter異常捕獲和Crash崩潰日誌收集Flutter
- IOS 崩潰日誌分析iOS
- Android 崩潰日誌採集元件-DhccCrashLibAndroid元件
- WWDC 2018:理解崩潰以及崩潰日誌
- Android 收集程式崩潰異常資訊Android
- win10查詢崩潰日誌方法 win10怎麼檢視崩潰日誌Win10
- crash日誌符號化,以分析崩潰符號
- Android 如何收集已釋出程式的崩潰資訊Android
- 崩潰日誌的欄位簡單說明
- iOS應用崩潰了,如何透過崩潰手機連線電腦查詢日誌方法iOS應用崩潰
- iOS安全–不同平臺的崩潰收集iOS
- 使用 google_breakpad 分析 Electron 崩潰日誌檔案Go
- iOS系統app崩潰日誌手動符號化iOSAPP符號
- 友盟崩潰日誌解析(2016.06.08)
- WkWebView 令人崩潰的崩潰WebView
- Android中Activity頻繁進入再退出時程式崩潰的解決方案Android
- Android Service入門到崩潰Android
- 我開源的Android日誌收集器Android
- rac日誌收集方法
- 日誌收集分析-heka
- HP收集日誌方法
- oracle 日誌收集工具Oracle
- Vector + ClickHouse 收集日誌
- HP日誌收集工具和收集方法
- Android 12 “致命”崩潰解決之路Android
- Android中處理崩潰異常Android
- WKWebView崩潰WebView
- Redis崩潰Redis
- 使用Kafka做日誌收集Kafka
- Android7.1.1Toast崩潰解決方案AndroidAST
- Crittercism:KitKat崩潰率0.7% iOS 7.1崩潰率1.6%iOS
- APP防崩潰APP
- 通過 Systemd Journal 收集日誌
- Linux-ELK日誌收集Linux
- TFA-收集日誌及分析
- TestBird 崩潰分析(Artisan) Android SDK 使用指南Android