tags:

views:

43

answers:

1

Dear All,

In Android I am writing an application in which, I would like to capture an event if in case user doesn't do any activity (tapping/key press).

For better context - It is like session timeout.

Basic solution - I can override the key press/tapping event and put one timer which continuously run either as thread or service. In my opinion this is bit heavier solution in terms of resource.

Any thoughts for elegant solution?

+1  A: 

Activity class fields:

private static final int KEY_TIMEOUT = 60000;
private long lastKeyEventTime;
private boolean checkKey;

in Activity.onCreate() :

checkKey = true;

Thread t = new Thread(){
    while (checkKey){
        if (lastKeyEventTime!=0 && System.currentTimeMillis()-lastKeyEventTime>TIMEOUT){
            // TIMEOUT
        }

        try {
          Thread.sleep(500);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
    }
}
t.start();

in Activity.onDestroy():

checkKey = false;

in Activity.onKeyDown():

lastKeyEventTime = System.currentTimeMillis();
radek-k