2015-01-12 3 views
3

나는 안드로이드 응용 프로그램이 있으며이 응용 프로그램이 시작될 때 응용 프로그램이 이전에 충돌했는지 여부를 알고 싶습니다. 이 충돌은 메모리 또는 다른 이유로 시스템을 저장하기 위해 앱의 OS에 의해 강제로 적용될 수 있습니다. UnhandledExceptionHandler에서 catch되지 않을 수 있습니다. 무엇 내가 지금까지 처리 한 것은 아래와 같습니다과 그 관련 기본 OS와 메모리 강제 경우지난번에 안드로이드 응용 프로그램이 추락했는지 확인하는 방법

UncaughtExceptionHandler handler = new UncaughtExceptionHandler(); 
Thread.setDefaultUncaughtExceptionHandler(handler); 

편집 캐싱되지 않습니다

제안하지 마십시오 제 3의 라이브러리를. 충돌을 잡기 후 때

+0

아래에 주어진하지 않을 경우 [이 질문] (HTTP를 살펴 보자 말할 것이다 변수를 얻을 수있는 공유 환경 설정의 도움으로

.com/questions/601503/how-do-i-get-crash-data-from-my-android 응용 프로그램), 이렇게하면 좋을 것 같습니다. –

+0

@G_V 그걸 들여다 보았지만 외장 라이브러리에이 보고서를 더하면 메모리 충돌 등의 OS 관련 충돌이 발생하지 않을 것입니다. –

+0

응용 프로그램에 알아 차리지 못했기 때문에 이전에 발생한 시스템 충돌을 감지하도록 요청할 수 없습니다 그것. 방금 OS/커널에 의해 죽었습니다. 당신이 묻는 것은 적어도 AFAIK는 불가능합니다. – shkschneider

답변

1

나는 해킹을 발견했고 그것은 나를 위해 일했습니다. 사용자가 앱을 종료했는지 또는 시스템을 종료했는지 또는 앱을 종료했는지 또는 앱 자체가 종료되었는지 여부를 알면 앱이 충돌했는지 확인할 수 있습니다. 앱 자체가 폐쇄 된 경우 사용자가 앱을 종료하거나 시스템을 종료하는 등의 경우에 충돌이 발생했다는 것을 의미합니다.// 유래 : 하나 저장하고 응용 프로그램이 충돌 않았거나 코드가

public class Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    boolean appcrashed=false; 
    super.onCreate(savedInstanceState); 
    boolean didUserLeft=loadSavedPreferences(); 
    appcrashed=!didUserLeft; 
    if(appcrashed) 
     Toast.makeText(this, "App Crashed!", Toast.LENGTH_LONG).show(); 
    else 
     Toast.makeText(this, "App OK!", Toast.LENGTH_LONG).show(); 
    savePreferences(false); 

    UnhandledExceptionHandler handler = new UnhandledExceptionHandler(); 

    Thread.setDefaultUncaughtExceptionHandler(handler); 

} 


public boolean loadSavedPreferences() { 
    SharedPreferences sharedPreferences = PreferenceManager 
      .getDefaultSharedPreferences(this); 
    boolean didUserLeft = sharedPreferences.getBoolean("didUserLeft", true); 
    return didUserLeft; 
} 

public void savePreferences(boolean value) { 
    SharedPreferences sharedPreferences = PreferenceManager 
      .getDefaultSharedPreferences(this); 
    Editor editor = sharedPreferences.edit(); 
    editor.putBoolean("didUserLeft", value); 
    editor.commit(); 
} 

@Override 
public void onResume(){ 
    super.onResume(); 
    savePreferences(false); 
} 

@Override 
public void onDestroy(){ 
    savePreferences(true); 
} 

@Override 
public void onPause() { 
    super.onPause(); // Always call the superclass method first 
    savePreferences(true); 
    } 


@Override 
public void onUserLeaveHint(){ 
    savePreferences(true); 
} 
1

이 그냥 MainActivitycrash라는 boolean 변수를 생성에 응용 프로그램을 입력하고 false의 값으로 SharedPreferences에 저장할 때 우선, SharedPreferences을 통해 일어날 것, 바로이 다시 저장 변수 값이 true이고 이는 자동으로 이전에 저장된 crash 값보다 우선합니다.

private void loadSavedPreferences() { 
    SharedPreferences sharedPreferences = PreferenceManager 
      .getDefaultSharedPreferences(this); 
    boolean crash = sharedPreferences.getBoolean("crash", false); 
    if(crash){ 
     // then your app crashed the last time 
    }else{ 
     // then your app worked perfectly the last time 
    } 
} 

그래서, 당신의 충돌 핸들러 클래스에서, 단지 true로 값을 저장 :

private void savePreferences(String key, String value) { 
    SharedPreferences sharedPreferences = PreferenceManager 
      .getDefaultSharedPreferences(this); 
    Editor editor = sharedPreferences.edit(); 
    editor.putBoolean("crash", false); 
    editor.commit(); 
} 

가 저장된 값을로드하려면 :

는 값을 저장하려면

ps 이것은 OS의 앱에서 처리되지 않은 모든 예외에 대해 실행되어야합니다. 안드로이드 죽이고 당신의 응용 프로그램을 다시 시작하면, 정적 변수가 null로 설정

public class CrashHandler extends Application{ 

    public static Context context; 

    public void onCreate(){ 
     super.onCreate(); 

     CrashHandler.context = getApplicationContext(); 
     // Setup handler for uncaught exceptions. 
     Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler() 
     { 
      @Override 
      public void uncaughtException (Thread thread, Throwable e) 
      { 
      handleUncaughtException (thread, e); 
      } 
     }); 

    } 

    public void handleUncaughtException (Thread thread, Throwable e) 
    { 
     e.printStackTrace(); // not all Android versions will print the stack trace automatically 

     SharedPreferences sharedPreferences = PreferenceManager 
       .getDefaultSharedPreferences(context); 
     Editor editor = sharedPreferences.edit(); 
     editor.putBoolean("crash", true); 
     editor.commit(); 

    } 

} 
+0

하지만 내 질문에 메모리 누수 및 기타 OS 생성 된 충돌이 위의 코드에서 그들을 잡는 방법을 찾지 못했습니다? –

+0

위의 코드로 인해 앱 충돌이 발생해야하며 위의 코드에 위배되지 않는 앱 충돌이 발생하면 로그에 대해 힌트를주세요. –

0

1) (더 정확하게, 처음에 null로 설정하고 조금 나중에, 곧, 초기 값으로 설정, 정적 초기화 할 수있다 아직 초기화되지 않은 변수에서 null 참조). 따라서 일부 정적 변수가 null로 설정되어있는 경우 번들의 데이터가 사용자가 무언가를하고 있다고 말하면 프로세스가 다시 시작됩니다 (onCreate (번들))에서 번들이 무엇인지 알고 있다고 가정합니다.

2) 영구 저장 장치에 플래그가있을 수 있습니다. 응용 프로그램이 시작되면 플래그는 true로 설정되고 정상적으로 완료되기 전에 false로 설정됩니다. 응용 프로그램이 시작될 때 해당 플래그가 참이면 충돌이 발생했습니다. (응용 프로그램이 정상적으로 닫힌 후에도 응용 프로그램이 충돌 할 가능성은 여전히 ​​있지만 ... 중요한 사항입니까?)

3) 앱의 pid를 영구 메모리에 저장할 수 있습니다 (myPid() 참조).

+0

하지만 사용자가 작업 표시 줄에서 응용 프로그램을 닫거나 사용자가 운영 체제를 종료하면 어떻게됩니까? –

+0

IMO이 두 가지는 프로세스를 죽이는 것과 다를 바 없습니다. 특히 사용자가 배터리를 제거하는 경우 특히 그렇습니다. 나는 종료시 앱에 onDestroy()가 호출 될 것으로 판단합니다. – 18446744073709551615