이 질문은 조금 오래 알고 있지만, 미래의 참조 및 Google 직원, 여기 찾기 어려운, 유래 질문 서로 완전한 대답 : Android App Restarts upon Crash/force close
unCaughtException
을 처리하는 데 사용되는 클래스를 생성 이 클래스 설정, 모든 스레드에서 그런
public class MyExceptionHandler implements
java.lang.Thread.UncaughtExceptionHandler {
private final Context myContext;
private final Class<?> myActivityClass;
public MyExceptionHandler(Context context, Class<?> c) {
myContext = context;
myActivityClass = c;
}
public void uncaughtException(Thread thread, Throwable exception) {
StringWriter stackTrace = new StringWriter();
exception.printStackTrace(new PrintWriter(stackTrace));
System.err.println(stackTrace);// You can use LogCat too
Intent intent = new Intent(myContext, myActivityClass);
String s = stackTrace.toString();
//you can use this String to know what caused the exception and in which Activity
intent.putExtra("uncaughtException",
"Exception is: " + stackTrace.toString());
intent.putExtra("stacktrace", s);
myContext.startActivity(intent);
//for restarting the Activity
Process.killProcess(Process.myPid());
System.exit(0);
}
}
()는 일반적으로 당신은 당신이 새 스레드 (비동기 또는 ...을 시작하지 않는 한 새로운 만들 수 있다면, 당연히 당신은 스레드에 대해 알고, 하나 있습니다) DefaultUncaughtExceptionHandler
Thread.setDefaultUncaughtExceptionHandler(new MyExceptionHandler(this,
YourCurrentActivity.class));
기억하십시오! 응용 프로그램을 개발하는 마지막 단계에서 수행하십시오. 예외를 처리하기 전에 하나씩 처리하십시오. DefaultUncaughtExceptionHandler
'ANR'또는 Activity Not 응답, 앱 충돌과 다릅니다 (강제 종료). 앱의 일부 작업이 특정 시간 동안 주 스레드 (UI 스레드)를 차단하고있는 것 같습니다.이 강제 종료/대기 대화 상자가 표시됩니다. 대기를 선택하면 해당 작업이 완료 될 때까지 앱이 응답하지 않습니다. 그런 다음 앱이 다시 시작됩니다. 그러나 일단 강제 종료를 누르면 다음에 액세스 할 때 응용 프로그램이 종료되고 다시 시작됩니다. – Abhijit
ANR의 경우가 아니며 때때로 "강제 종료"또는 "대기"가 표시됩니다. UI 스레드가 차단되고 있지만 때로는 응용 프로그램이 실제로 충돌하여 "강제 닫기"입니다. 그리고 응용 프로그램은 지속적으로 종료되지 않습니다 - 일부 플랫폼에 종료 않지만, 다른 사람에 그것은 단지 이전 지점에서 다시 시작하고 prefrences의 데이터를 모두 잃었다. – digerati32