5

Is there a way for my to program my app to automatically restart itself whenever it crashes? My app is just a simple media rendering App, however it occasionally crashes ( it's supposed to ). Is this at all possible? Thanks. My code looks like this

public void Play(){  if(mp != null) {
             mp.reset();
             mp.release();
             mp = null;
         }
AudioRenderer mr = new AudioRenderer(); 
mp = mr.AudioRenderer(filePath);
}

private class AudioRenderer extends Activity {
private MediaPlayer AudioRenderer(String filePath) {    
File location = new File(filePath);
Uri path = Uri.fromFile(location);
mp= MediaPlayer.create(this, path);
}
return mp
}
Sith
  • 105
  • 1
  • 4
  • 11
  • 4
    I'm really curious...why is the app supposed to crash? – Tenfour04 Aug 29 '11 at 03:16
  • 1
    Why don't you just wrap the code that is 'supposed to [crash]' in a try/catch and handle the error properly? – CrackerJack9 Aug 29 '11 at 14:49
  • @CrackerJack9 this is not efficient. In some case for eg,nullpointerexc, there is now way to handle it in catch. Restarting after it, is not good aproach. – alicanbatur Jan 14 '14 at 12:00
  • 1
    check [this article](http://chintanrathod.com/auto-restart-application-after-crash-forceclose-in-android/) to restart your application on any exception. – Chintan Rathod Jun 26 '15 at 07:45
  • Check out my solution here https://stackoverflow.com/a/73122395/9133569 – M.Ed Jul 26 '22 at 11:21

2 Answers2

14

this will do the job for you.

How to start the automatically stopped android service?

still i don't understand why it is supposed to crash.

UPDATE

you create an handler for uncaught exception

    private Thread.UncaughtExceptionHandler onRuntimeError= new Thread.UncaughtExceptionHandler() {
        public void uncaughtException(Thread thread, Throwable ex) {
            //Try starting the Activity again
    };

in your on create, you register an handler for uncaught exception

    @Override
    protected void onCreate() { 
        super.onCreate();
        Thread.setDefaultUncaughtExceptionHandler(onRuntimeError);  
    }
Community
  • 1
  • 1
Samuel
  • 9,883
  • 5
  • 45
  • 57
  • Thanks! I'm sure this will come in handy – Sith Aug 29 '11 at 01:17
  • 1
    This seems like a unreliable way to handle all uncaught errors, and would be a confusing experience for the user, who only sees the app suddenly navigate to the top activity again. Are there conditions that you wouldn't want to restart that activity? Are you sure you want to restart the home Activity on all uncaught errors? Is the app in an inconsistent state? What does this do to your Activity stack? Are you logging the exception? Agree with CrackerJack9: you should wrap the specific code that is crashing occasionally, log and fall through. Global handlers like this are generally a Bad Idea. – Dave Sims Mar 09 '12 at 22:01
  • 1
    @DaveSims, I sure agree with you, i did quote "why it is supposed to crash." still my response was specific to the problem raised to by the op. And for all the questions you raised, i assumed 'if' conditions inside 'onRuntimeError'. ;) – Samuel Mar 12 '12 at 00:49
6

I might be late a lot, but I found 2 in 1 solution for your problem.

public void doRestart() {
    Intent mStartActivity = new Intent(context, LoginActivity.class);
    int mPendingIntentId = 123456;
    PendingIntent mPendingIntent = PendingIntent.getActivity(context, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
    System.exit(0);
}

private void appInitialization() {
    defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
    Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler);
}

//make crash report on ex.stackreport
private Thread.UncaughtExceptionHandler defaultUEH;
// handler listener
private Thread.UncaughtExceptionHandler _unCaughtExceptionHandler = new Thread.UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        ex.printStackTrace();
        doRestart();
    }
};



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_index);
    appInitialization();