13

I am developing an application that will be running in Kiosk Mode. In this application, if the user didn't do anything in the application within 5 minutes, the application will show a screen saver that is the logo of the application.

My question is, how can I code on detecting IDLE within 5 minutes?

androidBoomer
  • 3,357
  • 6
  • 32
  • 42

4 Answers4

26

A BETTER SOLUTION HERE...... VERY SIMPLE

I used countdown timer as bellow:

private long startTime = 15 * 60 * 1000; // 15 MINS IDLE TIME
private final long interval = 1 * 1000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    countDownTimer = new MyCountDownTimer(startTime, interval);
}

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

    //Reset the timer on user interaction...
    countDownTimer.cancel();            
    countDownTimer.start();
}

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

    @Override
    public void onFinish() {
        //DO WHATEVER YOU WANT HERE
    }

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

CHEERS..........:)

Daniel
  • 2,415
  • 3
  • 24
  • 34
Melbourne Lopes
  • 4,817
  • 2
  • 34
  • 36
  • How do I cancel the countdown when the app exits this activity? I am using an intent in onFinish() and it restarts the timer when I startActivity() or If I click a button in this activity that takes me somewhere the timer is still active. – silversunhunter Feb 20 '15 at 01:51
  • 1
    To stop the countdowntimer when activity exits you can call countDownTimer.cancel() method in your onStop()....:) – Melbourne Lopes Feb 20 '15 at 07:52
  • Solution is good. Only drawback is found is that onUserInteraction() is called even when activity is getting paused. so if in onPause() I call countdownTimer.cancel(), before that a redundant method call countdownTimer.start() is getting made. – Akash Dubey Jun 26 '19 at 12:04
7

You should try this, It will Notify with a toast on detecting IDLE 5 minutes.

Handler handler;
Runnable r;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    handler = new Handler();
    r = new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            Toast.makeText(MainActivity.this, "user Is Idle from last 5 minutes",
                Toast.LENGTH_SHORT).show();
        }
    };
    startHandler();
}
@Override
public void onUserInteraction() {
     // TODO Auto-generated method stub
     super.onUserInteraction();
     stopHandler();//stop first and then start
     startHandler();
}
public void stopHandler() {
    handler.removeCallbacks(r);
}
public void startHandler() {
    handler.postDelayed(r, 5*60*1000);
}
Pradeep Gupta
  • 535
  • 5
  • 10
5

I think you could use http://developer.android.com/reference/android/app/Activity.html#dispatchTouchEvent(android.view.MotionEvent) and http://developer.android.com/reference/android/app/Activity.html#dispatchKeyEvent(android.view.KeyEvent) in your App to set a timestamp everytime a userinteraction takes place (simply override the methods and return false at the end so that the events will be propagated to underlying views) - then you can use some kind of timer which checks for the last timestamp of interaction recurringly and trigger your screen saver if your 5 minutes IDLE time are reached.

So in an Activity you simply override the before mentioned Methods like this:

@Override
public boolean dispatchTouchEvent (MotionEvent ev) {
   timestamp = System.getCurrentTimeMilis();
   return false; // return false to indicate that the event hasn't been handled yet
}

The dispatchKeyEvent and the other methods which you can override to determine user-activity should work fairly similar.

If you're using more than one Activity you may want to create a base class which extends Activity and Override all the dispatchXXXEvent you want to handle and which you than use as base class of all your Activities. But I guess the details of your implementation may be a little bit out of scope for the actual question :)

For the different possibilities of timers you may find useful info here: Scheduling recurring task in Android

Community
  • 1
  • 1
Foxtur
  • 153
  • 1
  • 6
  • Hi @Foxtur, can you give me example on how to use the dispatchTouchEvent()? – androidBoomer Jan 08 '14 at 02:30
  • 1
    @TwistedDroid Sorry I somehow linked the View Class instead of the Activity Class of the documentation - but that should be fixed now. I also added an example code - as I'm currently not at my workstation and I can't test the code myself I hope it works. Otherwise I'll follow up with an edit of the answer when I'm back at a machine with the Android SDK installed :) – Foxtur Jan 08 '14 at 03:55
1

try with:

private void startCount(int time) {

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

        @Override
        public void run() {

            // Add here the code for showing the fullscreenlogo

        }
    }, time);
}

then, whenever you want to start the count you should add:

startCount(time); // Replace time with 60*5*1000 for 5 mins

if you want to start the count when the app got minimized, then use this:

@Override
public void onPause() {
    super.onPause();
    startCount(time);
}
iGio90
  • 3,251
  • 7
  • 30
  • 43
  • startCount(time) will be triggered after the touch event. Even when the logo showed, when the user touch the screen, it will go back to the last activity that caught the idle. What code should I use? – androidBoomer Jan 08 '14 at 02:24
  • startCount(time); <-- add this to your ontouch/onclick. If you would like that the user return to the app when the screen is touched you will need for a service! – iGio90 Jan 08 '14 at 02:40