Boardy
Boardy

Reputation: 36217

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 home 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.

Upvotes: 0

Views: 194

Answers (3)

christoph
christoph

Reputation: 126

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

Upvotes: 0

Boardy
Boardy

Reputation: 36217

@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.

Upvotes: 1

George
George

Reputation: 1327

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*/
}

Upvotes: 0

Related Questions