8

I need to detect when user interacts with the phone and restart my app after 60 seconds from last user's touch on screen. Is is possible to do something like that? It must work as the screenserver for PC.

Gabrielle
  • 4,933
  • 13
  • 62
  • 122

3 Answers3

7

ACTION_USER_PRESENT is a broadcast action, so you should be able to write a broadcast receiver to respond to it and launch your application. Keep in mind that ACTION_USER_PRESENT is

sent when the user is present after device wakes up (e.g when the keyguard is gone).

I also just came across an example where the BOOT_COMPLETED broadcast action is used by a broadcast receiver to start an application on boot.

Gerrit
  • 852
  • 1
  • 9
  • 15
4

Is is possible to do something like that?

Only if your activity is in the foreground, in which case you can keep track of touch events. You cannot find out about touch events happening elsewhere in the system.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I found this solution : http://stackoverflow.com/questions/4481226/creating-a-system-overlay-always-on-top-button-in-android and it seems to work. I think it is not a good one, what do you think? – Gabrielle Jun 12 '13 at 13:55
  • @Gabrielle: That won't work on newer devices. To prevent tapjacking attacks, either *you* can get the touch events (and the underlying apps won't), or *they* can get the touch events (and you can't). – CommonsWare Jun 12 '13 at 14:05
  • You are right, I tested on a newer device and it is not working. Thank you! – Gabrielle Jun 12 '13 at 14:13
1

According to android lifecycle if user press home button or keypad locked onPause will get called.So do something like this.

@Override
public void onPause()
{
super.onPause();
Timer timer = new Timer();
TimerTask task = new TimerTask() {

    @Override
    public void run() {
        // TODO Auto-generated method stub
        finish();
    }
};
timer.schedule(task, 60000);
}

and if user comes before 60 seconds then in onRestart().

@Override
public void onRestart()
{
super.onRestart();
timer.cancel();
timer.purge();
}
mihirjoshi
  • 12,161
  • 7
  • 47
  • 78