bikash_binay
bikash_binay

Reputation: 219

How to implement application idle timeout in android application?

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.

Upvotes: 4

Views: 6214

Answers (2)

Nikolas Quemtri
Nikolas Quemtri

Reputation: 53

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.

Upvotes: 2

MSA
MSA

Reputation: 2482

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,

Upvotes: 2

Related Questions