10

Auto Restart application after Crash/Force Close in Android

 2 years ago
source link: https://chintanrathod.com/2015/04/27/auto-restart-application-after-crash-forceclose-in-android/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Auto Restart application after Crash/Force Close in Android

Byadmin April 27, 2015April 28, 2015

In an Android application, we usually got the “Force Closed” error if we didn’t get the exceptions right. Everyone has question about “How can I restart my application automatically if it force closed?”.

In this tutorial, we will learn how to handle exception manually and how to restart/auto launch your application after crash/force close.

To get Answer of your question is very simple. In that case you need to use Thread.setDefaultUncaughtExceptionHandler(). It will always enter in uncaughtException() in case your application crashed.

In order to restart your application when it crashed you should do the following thing.

Step 1

Create an Intent of your activity which you want to relaunch.

[java]
Intent intent = new Intent(activity, RelaunchActivity.class);

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_NEW_TASK);
[/java]

In this step, we take Intent to store which activity to launch with some flags.

Here,

Intent.FLAG_ACTIVITY_CLEAR_TOP is used because

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

Intent.FLAG_ACTIVITY_CLEAR_TASK is used because

Flag will cause any existing task that would be associated with the activity to be cleared before the activity is started.

Intent.FLAG_ACTIVITY_NEW_TASK is used because

If set, this activity will become the start of a new task on this history stack. A task (from the activity that started it to the next task activity) defines an atomic group of activities that the user can move to.

Step 2

Inside your uncaughtException() method, write following code.

[java]
PendingIntent pendingIntent = PendingIntent.getActivity(
YourApplication.getInstance().getBaseContext(), 0,
intent, intent.getFlags());

AlarmManager mgr = (AlarmManager) YourApplication.getInstance().getBaseContext()
.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, pendingIntent);

activity.finish();

System.exit(2);
[/java]

Didn’t get?

Here, PendingIntent is used. It is different then simple Intent. It stores request code and intent with its flags.

Read more about Intent flags.

AlarmManager is used to set an alarm to perform a task after 2 seconds. What we will do here is, we will start our application after 2 seconds. So we will set a timer to execute after 2 seconds and pass PendingIntent to it.

After that, we have to finish our current activity from where we got an exception. This is necessary step, also we need to exit from our application.

Full source code

[java]

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.Thread.UncaughtExceptionHandler;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.util.Log;

/**
* This custom class is used to handle exception.
*
* @author Chintan Rathod (http://www.chintanrathod.com)
*/
public class DefaultExceptionHandler implements UncaughtExceptionHandler {

private UncaughtExceptionHandler defaultUEH;
Activity activity;

public DefaultExceptionHandler(Activity activity) {
this.activity = activity;
}

@Override
public void uncaughtException(Thread thread, Throwable ex) {

Intent intent = new Intent(activity, RelaunchActivity.class);

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent pendingIntent = PendingIntent.getActivity(
YourApplication.getInstance().getBaseContext(), 0, intent, intent.getFlags());

//Following code will restart your application after 2 seconds
AlarmManager mgr = (AlarmManager) YourApplication.getInstance().getBaseContext()
.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000,
pendingIntent);

//This will finish your activity manually
activity.finish();

//This will stop your application and take out from it.
System.exit(2);

} catch (IOException e) {
e.printStackTrace();
}
}
}
[/java]

In this code YourApplication is an application class. To know more about it, please read Usage of Application class in Android

To use this class in your application, use following code inside onCreate() method of activity.

[java]
Thread.setDefaultUncaughtExceptionHandler(new DefaultExceptionHandler(this));
[/java]

Summary

In this tutorial, we learnt how to use UncaughtExceptionHandler in android application to handle exception manually. We also learnt how to restart application after crash or force close in Android.

This work by http://www.chintanrathod.com is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK