4

Is there a way to implement timeout feature in the following scenarios?

A web application with html pages and native screens.

1.When the application is in the background for 5 min -> destroy the application. 2.When the application is in the foreground but not receiving any user interaction for 5 min ->destroy the application.

SheetJS
  • 22,470
  • 12
  • 65
  • 75
bikash_binay
  • 219
  • 3
  • 12
  • This older post that might help you in that they had explain what you need to do to check system idle time [check this link](http://stackoverflow.com/a/4075857/1758960) – Gru Jul 03 '13 at 07:01
  • Why on earth would you want to do this? – Simon Jul 03 '13 at 07:21

2 Answers2

2

I think you can use this.

ApplicationConstants.TIMEOUT_IN_MS will be 300000 //5 min

private void timeout() {

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {

                    System.exit(0);//close aplication

        }
    }, ApplicationConstants.TIMEOUT_IN_MS);

}

    @Override
protected void onPause() {
    super.onPause();
    timeout();
    }

Cheers,

MSA
  • 2,502
  • 2
  • 22
  • 35
2

Regarding background state:

There is no need to kill the app's process manually by default. The Android OS does this by itself if there is a need to free up resources for the other applications.

See this guide for a reference.

Though if you need to perform some background work during this "idle time", you may start a Service to perform those operations and then stop it from code.

Regarding foreground state:

I think the best approach to use here is to send Messages to a Handler of the Main thread of your application, since you do not know if the user will interact with the UI again after they leave. When the user comes back to the UI, you may clear the message queue, using Handler's removeMessages method.

I do not recommend you to finish the process with System.exit(0) in Android.

Troy Stopera
  • 302
  • 3
  • 15