简体   繁体   中英

what to use for automatic signout of an App after 30 min in android

I want my application to Automatically signout after 30 minutes.so i am confused with lots of option i have. 1.handlers but i think if i use handler it won't calculate the display sleep time and i can also not able to resume the handler in another activity. 2.AlarmManager I dont know much about it but i think it use more cpu memory. 3.CountDownManager like how i used below

new CountDownTimer(40000, 1000) { //40000 milli seconds is total time, 1000 milli seconds is time interval

 public void onTick(long millisUntilFinished) {
  }
  public void onFinish() {
 }
}.start();

but can i resume CountDownManager process in another activity 4.timer in java it will create another thread

or is that nessesary that i should use service and include one of the above methods.because there are possibility that user can press home button and the app goes to onPause() state.or the display can sleep after some time.i also dont want my app to slow down .will using service will slow down my app.can anyone help me.

You can use a timeout, here is a simple example.

    public static final int TIMEOUT_FOR_APPLICATION = 1800000;//exit app after 30 minutes



    private void timeout() {

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {

        finish();//or whatever you want to do


        }
    }, TIMEOUT_FOR_APPLICATION);

}

Hope this is what are you looking for...

Cheers

use AlaramManager and set alarm after 30 minute. and you have to use pending intent for that so your service will be called after 30 minute. and you can do code there.

You can see How to set alarm programetically?

and if you wants to get idle time than if your gets touch or focus than you can reset that alarm.

and for screen off you can write code at onPause and onResume.

Add a field lastLogin in your class User.

private static final Long EXPIRE_TIME = 1000L * 60 * 30; //30M
private static final Long DELAY_TIME = 1000L * 60 * 5;
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
scheduledExecutorService.scheduleWithFixedDelay(new ContainerCleaner(), EXPIRE_TIME, DELAY_TIME, TimeUnit.MILLISECONDS);

public class ContainerCleaner implements Runnable {
    @Override
    public void run() {
        UserCache.cleanupContainer();
    }
}

in UserCache

public synchronized void cleanupContainer() {
    Collection<User> users= userCache.values();
    for (Userp : users) {
        if (isExpired(p)) {
            expire(p);
        }
    }
}
private boolean isExpired(SessionProfile sessionProfile) {
    if (sessionProfile == null) {
        return false;
    }
    Long lastAccess = sessionProfile.getValue(Name.LASTACCESS);
    return System.currentTimeMillis() - lastAccess > EXPIRE_TIME;
}

The code is from my webservice. It will detect if user isExpired every 5 mins.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM