Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when application is idle in Android

Tags:

android

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?

like image 589
androidBoomer Avatar asked Jan 08 '14 01:01

androidBoomer


2 Answers

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..........:)

like image 124
Melbourne Lopes Avatar answered Nov 06 '22 02:11

Melbourne Lopes


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);
}
like image 30
Pradeep Gupta Avatar answered Nov 06 '22 01:11

Pradeep Gupta