Question

I am having 10 different buttons in my application for different task to perform. I want to develop one service which continuously check (listens) and if user is not clicking any button for particular time let say for 5sec than i wish to perform some other task. How can I check that user has not clicked any button? If anyone having any idea please kindly let me know.

Was it helpful?

Solution

In each of your click listeners save off the time the last button was clicked:

private long lastClickTimestamp;
private Handler handler = new Handler();

public void onCreate( Bundle saved ) {
   BackgroundJob job = new BackgroundJob();
   handler.postDelayed( job, SECONDS_TO_WAIT * 1000 );

   button1.setClickListener( new OnClickListener() {
       public void onClick( View view ) {
           lastClickTimestamp = System.currentTimeInMillis();
           // do the listener logic for button 1 here.
       }
   });

   button2.setClickListner( new OnClickListener() {
       public void onClick( View view ) {
           lastClickTimestamp = System.currentTimeInMillis();
           // do the listener logic for button 2 here.
       }
   });

   // repeat that for all 10 buttons.
}

Now the smarter developer would create a reusable base class that handled setting the timestamp once, then reuse that base class in each of the 10 buttons. But, that's left up to you. Then the background job would look like:

public class BackgroundJob implements Runnable {

   private boolean done = false;

  // meanwhile in job:
  public void run() {

     if( lastClickTimestamp > 0 && System.currentTimeInMillis() - lastClickTimestamp > SECONDS_TO_WAIT * 1000 ) {
       // let's do that job!
     }

     if( !done ) {
        // reschedule us to continue working
        handler.postDelayed( this, SECONDS_TO_WAIT * 1000 ); 
     }
  }
}

If you have to use a service you can send a notification to the service saying a button was clicked, then the service can keep track of the time when that occurred. I wouldn't use a service for this because playing an animation or sound doesn't need to survive if the app is put into the background or killed. Services are meant for things like playing music when someone is doing something else, chat applications, or things that need to run in the background when the user isn't interacting with the application. What you're describing could be done as I've shown because when the user gets a phone call or text message they'll leave your application, and the animation or sound you're playing probably should stop too. Pretty easy to do with the Handler option I showed. More difficult, but doable, with a service.

OTHER TIPS

You could simply set a Timer to the desired length. When a button is clicked, just reset the timer. Start the timer in onResume so it starts even if the user is coming back from a phone call or other activity. You should probably stop the timer in onPause of the activity too.

On each button click, update some Calendar object to a new Calendar instance and then you can check what the time is of that Calendar and see if it's more than 5 minutes ago.

//this is a field
Calendar calendar;
public void onClick(View v) {
calendar = Calendar.getInstance();
//etc
switch(v.getId()) {
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top