22

In my application there are three activities A -> B -> C -> A. I want to detect application's idle time, so that after 15 mins it will pop up a message irrespective of activity. what is the best method to implement this.

Harikrishnan R
  • 1,444
  • 4
  • 16
  • 27

3 Answers3

63

I would be doing it in this way:

  1. Create thread which will control idle activity
  2. Run this thread in Application environment
  3. At each user interaction just refresh idle time

Class for storing global Thread which will control idle time

public class ControlApplication extends Application
{
    private static final String TAG=ControlApplication.class.getName();
    private Waiter waiter;  //Thread which controls idle time

    // only lazy initializations here!
    @Override
    public void onCreate()
    {
        super.onCreate();
        Log.d(TAG, "Starting application"+this.toString());
        waiter=new Waiter(15*60*1000); //15 mins
        waiter.start();
    }

    public void touch()
    {
        waiter.touch();
    }
}

Class which will be parent for all of your activities

public class ControlActivity extends Activity
{
    private static final String TAG=ControlActivity.class.getName();

    /**
     * Gets reference to global Application
     * @return must always be type of ControlApplication! See AndroidManifest.xml
     */
    public ControlApplication getApp()
    {
        return (ControlApplication )this.getApplication();
    }

    @Override
    public void onUserInteraction()
    {
        super.onUserInteraction();
        getApp().touch();
        Log.d(TAG, "User interaction to "+this.toString());
    }

}

And finally Thread itself

public class Waiter extends Thread
{
    private static final String TAG=Waiter.class.getName();
    private long lastUsed;
    private long period;
    private boolean stop;

    public Waiter(long period)
    {
        this.period=period;
        stop=false;
    }

    public void run()
    {
        long idle=0;
        this.touch();
        do
        {
            idle=System.currentTimeMillis()-lastUsed;
            Log.d(TAG, "Application is idle for "+idle +" ms");
            try
            {
                Thread.sleep(5000); //check every 5 seconds
            }
            catch (InterruptedException e)
            {
                Log.d(TAG, "Waiter interrupted!");
            }
            if(idle > period)
            {
                idle=0;
                //do something here - e.g. call popup or so
            }
        }
        while(!stop);
        Log.d(TAG, "Finishing Waiter thread");
    }

    public synchronized void touch()
    {
        lastUsed=System.currentTimeMillis();
    }

    public synchronized void forceInterrupt()
    {
        this.interrupt();
    }

    //soft stopping of thread
    public synchronized void stop()
    {
        stop=true;
    }

    public synchronized void setPeriod(long period)
    {
        this.period=period;
    }

}
Mithun Sreedharan
  • 49,883
  • 70
  • 181
  • 236
Barmaley
  • 16,638
  • 18
  • 73
  • 146
5

So I would personally use the AlarmService. You can specify a PendingIntent that will start an activity which displays a dialog. After any event which should restart the timer you just cancel the pendingIntent and reregister it.

Greg Giacovelli
  • 10,164
  • 2
  • 47
  • 64
  • Same way I did it, its working for me. Which are the events to capture for reregistering the timer. – Harikrishnan R Nov 02 '10 at 07:22
  • So that's up to your application and definition of idle. Easiest would be to use a broadcast intent that you fire whenever you feel the user is no longer idle. In your receiver reset the timer. – Greg Giacovelli Nov 02 '10 at 07:55
  • For what events do you restart your timer? We're trying to implement this in our project and have it reset based on touch events, but Android's touch dispatching system doesn't really allow for this. – Karakuri Jun 27 '12 at 22:06
  • There is an event inside of activity called, onUserInteraction() that is sort of reliable. So whenever it is called, you can have it reset. It should not only take into account touch events, but cursor events as well. I still would go with this approach since adhoc threads usually never end well. I would even use a Handler if the AlarmService was too heavy weight for the situation. – Greg Giacovelli Jul 03 '12 at 04:45
  • http://stackoverflow.com/questions/576600/lock-android-app-after-a-certain-amount-of-idle-time – jrh Mar 10 '14 at 16:07
2

I think CountDownTimer is the best way to do this.

    public class IdleCountDownTimer extends CountDownTimer {
        public IdleCountDownTimer(long startTime, long interval) {
            super(startTime, interval);
        }

        @Override
        public void onFinish() {
            //Time lapsed 
           Toast.makeText(getApplicationContext(),"The text you want to display",Toast.LENGTH_LONG)

        }

        @Override
        public void onTick(long millisUntilFinished) {
        }
    } 

Now declare the global object

IdleCountDownTimer idleCountDownTimer;

Now initialize and start the count down.

@Override
public void onCreate()
{
    super.onCreate();
    idleCountDownTimer = new IdleCountDownTimer(seconds * 60 * 1000, interval_in_seconds * 60 * 1000);
    idleCountDownTimer.start();
}

Now call cancel and start when you want to reset the count down.

Eg 1 : for all key, touch, or trackball events

@Override
public void onUserInteraction(){
    super.onUserInteraction();

    //Reset the count down
    idleCountDownTimer.cancel();             
    idleCountDownTimer.start();
}

Eg 2 : For Touch events only

@Override
public boolean onTouchEvent(MotionEvent event) {
        //Reset the count down
        idleCountDownTimer.cancel();             
        idleCountDownTimer.start();

        // Let the event flow
        return false;
}
Nalin
  • 135
  • 7