How can I listen to downtime on the screen?

I need to use the standby listener to listen when the user is using the application or idle when the activity is active. I need to do something when the user does not use the application for more than ten seconds. How can I make this possible?

+6
source share
1 answer

Here's how you can achieve this:

First, you need Runnable (), which will be launched when a timeout occurs (e.g. 10 seconds). The following is Runnable ():

private Runnable DoOnTimeOut = new Runnable() { public void run() { // Do something Here } } 

Now in your activity you can call postDelayed for DoOnTimeOut:

 Handler hl_timeout = new Handler(); @Override public void onCreate(Bundle b) { hl_timeout.postDelayed(DoOnTimeOut, 10000); // The DoOnTimOut will be triggered after 10sec } 

Now the most important part is that when you see the user interaction, you want to cancel the DoOnTimeOut call, and then set the call again for the next 10 seconds. The following is a method to override your activity for user interaction:

 @Override public void onUserInteraction() { super.onUserInteraction(); //Remove any previous callback hl_timeout.removeCallbacks(DoOnTimeOut); hl_timeout.postDelayed(DoOnTimeOut, 10000); } 

I hope this will be helpful to you.

+7
source

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


All Articles