안드로이드 개발시 비정상 종료에 대해 질문입니다.

조회수 1930회

APP 실행시 비정상 종료 Exception 발생시 앱이 죽곤 하는데

UncaughtExceptionHandler 사용하면 Exception 발생하면 그 클래스로 이동해

로그를 찍거나 재시작이 가능하다는 이야기를 들었습니다.

구글링 하여 예제를 해봤는데 로그까지는 잘찍는데 재시작까지는 안되는거 같습니다.

반복해서 계속 로그만 찍지 그이상 이동안하는거 같습니다.

예제 코드입니다.

private static final String TAG = Common_DTO.class.getSimpleName();

private Thread.UncaughtExceptionHandler mDefaultUncaughtExceptionHandler;


public Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler()
 {
    return mDefaultUncaughtExceptionHandler;
}

public void onCreate()
{
    super.onCreate();
    mDefaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
    Log.d("MainApplication", "" + mDefaultUncaughtExceptionHandler);

    Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler());
}

private class UncaughtExceptionHandler implements Thread.UncaughtExceptionHandler 
{

    @Override
    public void uncaughtException(Thread t, Throwable e) 
    {
        Log.d(TAG, "Crash!!!");

        // Try everything to make sure this process goes away.
         android.os.Process.killProcess(android.os.Process.myPid());
         System.exit(0);
        Intent restartIntent = new Intent(getApplicationContext(), IntroActivity.class);
        PendingIntent runner = PendingIntent.getActivity(getApplicationContext(), 99, restartIntent, PendingIntent.FLAG_ONE_SHOT);
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 5000, runner);
    }
}

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)