开发者

Android Best Way to Detect and Handle User INACTIVITY

开发者 https://www.devze.com 2023-02-26 07:09 出处:网络
Inactivity is a very important EVENT.For many apps if the user does not interact with it for a certain number of seconds its time to reset the app and go back to main activity logout, or conserve powe

Inactivity is a very important EVENT. For many apps if the user does not interact with it for a certain number of seconds its time to reset the app and go back to main activity logout, or conserve power. So I would really like to get some feedback on the best way to detect this. In fact I think everyone will benefit from a good solution to this.

So my question is twofold:

1) Is there a better way to detect user inactivity than using a combination of activity.onUserInteraction() to reset a CountDownTimer?

Note: One reported downside to t开发者_如何学编程his approach is that softkeypad interaction might not be caught by this approach.

Note: Another reported downside is the CountDownTimer is off main thread and might not update correctly. I am not sure how big an issue this is?

Note: CountDownTimer appears to have cancellation issues as well: how to stop/cancel android CountDownTimer

2) Lets say that onUserInteraction()/CountDownTimer is the best/only solution to this problem there are still some questions:

a) should each activity launch its own countdown timer?

b) Should a single countdown timer be restarted in the onCreate method of each activity?

c) lets say I want to dim the screen or goto main activity when the countdown expires where should the timeout handler be located? In each activity? In a service?

Thanks


Just stumbled upon this question as I've answered something similar just now.

Personally, I'd opt for option 2 that you have suggested, and put a timer into a singleton so its available across all activities. Theres no need for a separate countdown timer unless you have a specific requirement to react different under different features of your application.

Why would you want to reset the timer in the onCreate? You should do that each time the user interacts with the application, such as in the activity.onUserInteraction() method.

To quote from my previous answer:

You'll need to invest a little thought into exactly what your requirements are here, but from what I can tell, you want to keep track of the user interactions and if a time limit expires since the last interaction, perform some action, in your case logging them out of your application.

Firstly, you'll need some place that you can track when the last interaction occured, since you'll want this to be application wide you could use a singleton to hold this, or override the Application class, either way should do.

Next, you'll need to start tracking user interactions. From your activities, you can override the onUserInteraction method, this gets invoked anytime the user interacts with the application such as key event. Each time you hit this method, update your singleton and let it know something has happened, with a timestamp.

Finally, you'll need some kind of looping check to constantly check if anything has happened recently. Theres various was of doing this, you could have a continuous loop that compares current timestamp to the last recorded event, a bit of draft code :

while(true)
{
   if (timeLastEventRecorded < (now - 15))
   {
      //nothing has happened in 15 minutes, so take corrective action
   }
}

Presumably you'll already have some code in your application that takes care of logouts, such as when the user clicks "logout", you should just be able to invoke that in the sample above.

0

精彩评论

暂无评论...
验证码 换一张
取 消