I have an android application and i want to know on the startup of this application whether my application crashed previously or not. This crash may be crash enforced by OS on app for saving memory or any other reason. It may not be caught in UnhandledExceptionHandler. What i have handled so far is given below and it is not caching those native os related and memory enforced cases
UncaughtExceptionHandler handler = new UncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(handler);
EDIT:
Please don't suggest 3rd party libraries.
This would be happen via SharedPreferences
, first of all when you just enter your app in the MainActivity
create a boolean
variable called crash
and save it to your SharedPreferences
with a value of false
, then when catching a crash, just resave this variable with the value true
and this will automatically override the crash
value stored before.
To save 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 the 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 crash handler class, just save the value to true:
p.s. this must run for all unHandled Exceptions whatever from the app of 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();
}
}
I have found a hack and it worked for me. One can check if its app was crashed if one knows whether user left the app or shut down the system or did any such thing or the app itself got closed. If the app itself got closed it means that it was crashed otherwise it wasn't (in cases such as user closing app or shutting down system).
With help of shared preferences one can store and get a variable which will tell if app 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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With