开发者

Perform action if app is not used for specified time in android

I have an android app in development which enables the user to protect the data inside the app by entering a password or pin. Is there a method to check if the app hasn't been used for lets say 10 minutes to then change a setting in the app prefs file.

I was thinking of maybe a service, or a class within the app that runs instead another thread with a timer but not sure if this would be suitable.

Another method, I was thinking is there a way so that when the user presses the home button, the hom开发者_开发技巧e button does its normal task of return to the android home screen but it also changes a setting within the apps prefs file.

Thanks for any help you can provide.


@Joseph Earl comment on my question is the answer. Works perfectly.

Use the onPause method and get the time of android and save it to the app config.

then use the onResume to get the current time of android and the time from the config and get the time in minutes.

Works perfectly.


you can use something like this

private long lastTimeUpdating;

all where you have an actions

lastTimeUpdating = System.currentTimeMillis();

and you must have a thread which is updating every 50 milliseconds, in this thread check this

if(System.currentTimeMillis() - lastTimeUpdating >= 5 * 60 * 1000) {
   /*change your settings here*/
}


You can use the Chronometer control for timing an action.

Use the start() and stop() methods, to determine state.

Example:

final Chronometer timer = 
    (Chronometer) findViewById(R.id.Chronometer1);
long base1 = timer.getBase();
timer.setBase(0);
timer.start();

listen for changes using the Chronometer.OnChronometerTickListener interface

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜