Notice
Recent Posts
Recent Comments
Link
투케이2K
699. (Android/Java) Thread.UncaughtExceptionHandler 사용해 앱 사용 중 알 수 없는 에러 이벤트 감지 - App Used Error 본문
Android
699. (Android/Java) Thread.UncaughtExceptionHandler 사용해 앱 사용 중 알 수 없는 에러 이벤트 감지 - App Used Error
투케이2K 2023. 11. 27. 19:23[개발 환경 설정]
개발 툴 : AndroidStudio
[소스 코드]
package com.example.javaproject;
import android.app.Activity;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class S_Crash implements Thread.UncaughtExceptionHandler {
/**
* // --------------------------------------------------------------------------------------
* TODO [클래스 설명]
* // --------------------------------------------------------------------------------------
* 1. 앱 사용 중 발생한 Crash 크래시 리포트 저장 클래스
* // --------------------------------------------------------------------------------------
* 2. 호출 방법 : 각 액티비티 onCreate 에 선언 필요
*
* @Override
* protected void onCreate(Bundle savedInstanceState) {
* super.onCreate(savedInstanceState);
*
* // TODO [앱 크래시 리포트 저장]
* Thread.setDefaultUncaughtExceptionHandler(new S_Crash(this));
* }
*
* // --------------------------------------------------------------------------------------
* 3. 로그 저장 예시 :
*
* ----------------------------------------------------
* [FILE SAVE TIME] : 2023-11-24 14:31:27 금요일
* ----------------------------------------------------
* [SAVE EXPLAIN] : uncaughtException : 예기치 못한 Exception 크래시 로그 저장
* ----------------------------------------------------
*
* java.lang.NumberFormatException: For input string: "h"
*
* java.lang.Integer.parseInt(Integer.java:747)
* java.lang.Integer.parseInt(Integer.java:865)
* com.example.javaproject.A_Intro.onCreate(A_Intro.java:364)
* android.app.Activity.performCreate(Activity.java:8591)
* android.app.Activity.performCreate(Activity.java:8570)
* android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1384)
* android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4150)
* android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4325)
* android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101)
* android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
* android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
* android.app.ActivityThread$H.handleMessage(ActivityThread.java:2574)
* android.os.Handler.dispatchMessage(Handler.java:106)
* android.os.Looper.loopOnce(Looper.java:226)
* android.os.Looper.loop(Looper.java:313)
* android.app.ActivityThread.main(ActivityThread.java:8762)
* java.lang.reflect.Method.invoke(Native Method)
* com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:604)
* com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
*
* ----------------------------------------------------
*
* // --------------------------------------------------------------------------------------
* */
/**
* // --------------------------------------------------------------------------------------
* // TODO [빠른 로직 찾기 : 주석 로직 찾기]
* // --------------------------------------------------------------------------------------
*
* // --------------------------------------------------------------------------------------
*
* // --------------------------------------------------------------------------------------
*
* // --------------------------------------------------------------------------------------
* */
// -----------------------------------------------------------------------------------------
// TODO [전역 변수 선언 실시]
// -----------------------------------------------------------------------------------------
private Thread.UncaughtExceptionHandler crashHandler;
private Activity activity = null;
// -----------------------------------------------------------------------------------------
// TODO [클래스 생성자 초기화]
// -----------------------------------------------------------------------------------------
public S_Crash(Activity app) {
this.crashHandler = Thread.getDefaultUncaughtExceptionHandler();
this.activity = app;
}
// -----------------------------------------------------------------------------------------
// TODO [예기치 못한 Exception 이벤트 감지 부분]
// -----------------------------------------------------------------------------------------
public void uncaughtException(Thread t, Throwable e) {
if (this.activity != null){
// [변수 선언 실시]
String content = "";
// [로직 처리 실시]
try {
// [날짜 데이터 확인 실시]
String saveTime = "";
long time = 0;
try {
time = System.currentTimeMillis();
SimpleDateFormat dayTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E요일", Locale.KOREA);
saveTime = dayTime.format(new Date(time));
}
catch (Exception ej){}
// [Exception 예외 상황 로그 확인]
// -----------------------------------------
//*
StringWriter err = new StringWriter();
e.printStackTrace(new PrintWriter(err));
String report = "\n" + err.toString() + "\n\n";
// */
// -----------------------------------------
//*
StackTraceElement[] arr = e.getStackTrace();
report = report + "\n\n";
Throwable cause = e.getCause();
if(cause != null) {
report += cause.toString() + "\n\n";
arr = cause.getStackTrace();
for (int i=0; i<arr.length; i++) {
report += " "+arr[i].toString()+"\n";
}
}
// */
// -----------------------------------------
// [로그 출력 형식 포맷]
content = content + "\n" + "\n" + "\n";
content = content + "----------------------------------------------------" + "\n";
content = content + "[FILE SAVE TIME] : " + String.valueOf(saveTime) + "\n";
content = content + "----------------------------------------------------" + "\n";
content = content + "[SAVE EXPLAIN] : uncaughtException : 예기치 못한 Exception 크래시 로그 저장" + "\n";
content = content + "----------------------------------------------------" + "\n";
content = content + report + "\n";
content = content + "----------------------------------------------------" + "\n";
content = content + "\n" + "\n" + "\n";
// [로그 출력]
}
catch (Exception eh){
}
}
if (this.crashHandler != null){
this.crashHandler.uncaughtException(t, e);
}
}
} // TODO [클래스 종료]
반응형
'Android' 카테고리의 다른 글
Comments