How to implement application downtime in an Android application?

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

Web application with html pages and native screens.

1. When the application is in the background for 5 minutes → destroy the application. 2. When the application is in the foreground, but does not receive any user interaction for 5 minutes → destroy the application.

+4
source share
2 answers

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(); } 

Greetings

+2
source

Regarding the background state:

By default, there is no need to kill the application process manually. Android OS does this on its own if there is a need to free resources for other applications.

See this guide for reference.

Although if you need to do some background work during this "downtime", you can start the Service to perform these operations and then stop it from the code.

Regarding the foreground state:

I think that the best approach to use here is to send messages to the Handler of the main thread of your application, because you do not know if the user will interact with the user interface after he leaves. When the user returns to the user interface, you can clear the message queue using the Handler removeMessages method.

I do not recommend terminating the process using System.exit (0) on Android.

+2
source

Source: https://habr.com/ru/post/1489419/


All Articles