투케이2K

131. (TWOK/UTIL) [Android/Java] S_Crash : 앱 사용 중 발생한 Crash 크래시 리포트 저장 클래스 - UncaughtException 본문

투케이2K 유틸파일

131. (TWOK/UTIL) [Android/Java] S_Crash : 앱 사용 중 발생한 Crash 크래시 리포트 저장 클래스 - UncaughtException

투케이2K 2024. 7. 19. 19:27
반응형

[설 명]

프로그램 : Android / Java

설 명 : S_Crash : 앱 사용 중 발생한 Crash 크래시 리포트 저장 클래스 - UncaughtException

 

[소스 코드]

 

package com.example.javaproject;

import android.app.Activity;
import android.content.Context;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Arrays;
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 금요일
     * ----------------------------------------------------
     * [DEVICE MODEL NAME] : [samsung] SM-S906N
     * ----------------------------------------------------
     * [DEVICE OS VERSION] : 13
     * ----------------------------------------------------
     * [APP VERSION] : 1.0.2
     * ----------------------------------------------------
     * [APP CODE] : 2
     * ----------------------------------------------------
     * [SAVE EXPLAIN] : uncaughtException : 예기치 못한 Exception 크래시 로그 저장
     * ----------------------------------------------------
     * [CLASS NAME] : ComponentInfo{com.example.javaproject/com.example.javaproject.A_Intro}
     * ----------------------------------------------------
     *
     * 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 [빠른 로직 찾기 : 주석 로직 찾기]
     * // --------------------------------------------------------------------------------------
     * [SEARCH FAST] : [파일] : [예기치 못한 Exception 이벤트 감지 부분]
     * // --------------------------------------------------------------------------------------
     * [SEARCH FAST] : [파일] : [로그 정보 파일 데이터 저장]
     * // --------------------------------------------------------------------------------------
     *
     * // --------------------------------------------------------------------------------------
     * */







    // -----------------------------------------------------------------------------------------
    // TODO [전역 변수 선언 실시]
    // -----------------------------------------------------------------------------------------
    private Thread.UncaughtExceptionHandler crashHandler;
    private Activity activity = null;






    // -----------------------------------------------------------------------------------------
    // TODO [클래스 생성자 초기화]
    // -----------------------------------------------------------------------------------------
    public S_Crash(Activity app) {
        this.crashHandler = Thread.getDefaultUncaughtExceptionHandler();
        this.activity = app;
    }





    // -----------------------------------------------------------------------------------------
    // TODO [SEARCH FAST] : [파일] : [예기치 못한 Exception 이벤트 감지 부분]
    // -----------------------------------------------------------------------------------------
    public void uncaughtException(Thread t, Throwable e) {

        if (this.activity != null){
            S_Log._E_("예기치 못한 Exception 이벤트 감지", new String[]{ String.valueOf(e.getMessage()) });


            // [로직 처리 실시]
            try {

                S_FileManager.appCrashLogSave(this.activity, "uncaughtException : 예기치 못한 Exception 크래시 로그 자동 저장", e);

            }
            catch (Exception eh){
            }

        }

        if (this.crashHandler != null){
            this.crashHandler.uncaughtException(t, e);
        }

    }





    // -----------------------------------------------------------------------------------------
    // TODO [SEARCH FAST] : [파일] : [로그 정보 파일 데이터 저장]
    // -----------------------------------------------------------------------------------------
    /*
    S_Crash.catchException(getApplicationContext(), e); // [로그 파일 저장]
    // */
    // -----------------------------------------------------------------------------------------
    public static void catchException(Context mContext, Throwable e){

        /**
         * // -----------------------------------------
         * 1. 에러 정보 파일 데이터 저장
         * // -----------------------------------------
         * 2. 호출 방식 :
         *
         *   S_Crash.catchException(A_Main.this, e); // [로그 파일 저장]
         *
         * // -----------------------------------------
         * 3. 설 명 : 내부 저장소는 퍼미션 권한 없이 읽기, 쓰기 가능 / 앱 삭제 시 저장된 데이터 삭제
         * // -----------------------------------------
         * 4. 저장 공간 : /data/data/패키지 명칭/files/APP_CRASH_LOG_FILE
         * // -----------------------------------------
         * */


        // TODO [mContext != null]
        //*
        if (mContext != null){

            S_FileManager.appCrashLogSave(mContext, "caughtException : Try Catch Exception 예외 처리 로그 자동 저장", e);

        }
        // */

    }


} // TODO [클래스 종료]

 

반응형
Comments