开发者

Timeout screen if user is inactive in an Android application

I would like to be able to detect if a user is inactive in my Android application for a specific period of time, if so I want to start a new activity where the user is asked for a password to resume using the application. The previou开发者_如何学编程s activity should start off where it was left.

Is this possible in an Android application? I am guessing I should start off a separate thread as soon as the application loads, which keeps checking for user activity, but how do I do that.


This is a very difficult question to answer without knowing details of your Application.

For example...how many Activities does the app have? What do those Activities do? What widgets (buttons, listviews etc etc) does each have?

For a start, to gauge whether a user is 'active' or not then you'd need to keep a 'last used' time field. Doing this however would mean keeping a static 'long' variable (either at the Application level or persisted through SharedPreferences if there is more than one Activity). The variable would hold the 'last used' time in milliseconds.

You would need to update this every time the user interacted with any Activity (touched, scrolled, clicked a button etc). Those interactions require different listeners which would each have to update the 'last used' time. More than that, you'd need each listener to test the 'last used' variable before updating it with the current time - if it was longer ago than 5 minutes (or whatever your timeout) then they'd need to raise an event to force a popup requiring the user enters the password.

This is all doable but it's complex.

Alternatively, you could simply set a countdown timer but you'd still need every touch, click or gesture to reset the countdown and it would still require doing this for every Activity in your app. Pretty much most of the above also applies.

Describing your app further might help someone provide a more concise answer.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜