Android Best Way to Detect and Handle User INACTIVITY
Asked Answered
C

1

18

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 this 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

Congruent answered 16/4, 2011 at 21:3 Comment(6)
Please note most apps have multiple activities and timeout could really occur on any activity.Congruent
Regarding conserving power, the OS will handle this for you, and if you program within the intention of Android you should be fine. As for the need to 'reset' the app I guess it is feasible that might fall within the design for some apps but I can't see it being that common.Cabriole
There are straightforward and standard mechanisms that exist that will tell you if the user has switched to another app, or the device is going into standby. Bear in mind if the user stops interacting with any app the phone will go into standby. Can you describe a scenario where these mechanisms will be inadequate to detect meaningful user inactivity with an app?Cabriole
Could you describe how they could be used to detect when an app has been inactive for 95 seconds? with callbacks when this occurs and no blanking of the screen?Congruent
did you checked this #4209230Insignificant
There is a library I developed to help with this use case. Please feel free to take a look. github.com/jose96043/TimezOutMononucleosis
V
4

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.

Vincenza answered 12/10, 2011 at 19:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.