How to know if an Android app was last crashed

I have an Android application and I want to know about running this application, whether my application has been crashed before or not. This crash can lead to a crash caused by the OS in the memory saving application or for any other reason. It cannot be caught in the UnhandledExceptionHandler. What I have handled so far is below and it does not cache these native and memory related cases

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

      

EDIT:

Please do not suggest third party libraries.

+3


source to share


3 answers


I found a hack and it worked for me. It is possible to check if his application was crashed if someone knows if the user left the application or closed the system or did something similar, or the application closed. If the application is closed, it means that it was crashed, otherwise it was not (in cases such as closing the application or shutting down the system).

With the help of general settings, you can save and get a variable that will tell you if the application was crashed or not, the code is given below



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


source


This will happen through SharedPreferences

, first of all, when you just type your application in MainActivity

, create a variable boolean

with a name crash

and store it in SharedPreferences

with a value false

, then when you catch a crash, just store this variable with a value true

and this will automatically override the previously saved value crash

.

To store the value:

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

      

To load a saved value:



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
    }
}

      

So, in your fault handler class, just store the value to true:

ps this should be done for all thrown exceptions regardless of the application from the OS.

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();

    }

}

      

+1


source


1) When Android kills and restarts your application, static variables are set to zero (more precisely, they are initially set to zero and a bit later, very soon, are set to their initial values, the static initializer may see zero in variables that have not yet been initialized). So if some static variable is set to null, while the data in the Bundle says that the user was doing something, the process was restarted (I assume you know what the Bundle is for onCreate ( Bundle )).

2) You can have a flag in persistent storage; the flag will be, say, set to true when the application starts and will be set to false before it exits normally. If this flag is true when the application starts, a crash has occurred. (There is still a small chance that the application will crash after it exits normally ... But is this important to you?)

3) You can store the application pid in persistent memory (see myPid () ).

0


source







All Articles