nomnom
nomnom

Reputation: 1638

In Android how can I disable a Listener for a while after an event has been caught?

For example:

private final SensorEventListener mySensorEventListener = new SensorEventListener() {
   @Override
   public void onSensorChanged(SensorEvent se) {
      // pseudo code
      if (it is a shake event) {
         output some text;
      }
   }
}

I have implemented a SensorEventListener, but the shake events are detected directly one after another. What can I do to make the listener rest for a while after a shake event is detected and some text is output?

Upvotes: 1

Views: 1608

Answers (3)

SathMK
SathMK

Reputation: 1171

You can use

sensorManager.unregisterListener(mySensorEventListener,sensor);

For more info refer this link here

Upvotes: 2

sroes
sroes

Reputation: 15053

Either disable the click listener by setting it to null, or keep track of the time the last shake was processed:

private final SensorEventListener mySensorEventListener = new SensorEventListener() {

   private Date lastShaked;

   @Override
   public void onSensorChanged(SensorEvent se) {
      // pseudo code
      if (lastShaked + some time < now) {
         lastShaked = now;
         output some text;
      }
   }
}

Upvotes: 1

berserk
berserk

Reputation: 2728

To disable listener(for example OnClickListener from some object), use:

object.setOnClickListener(null);

If you need to enable listener again, use:

object.setOnClickListener(YourActivity.this);

After a shake, disable the listener till some event happens, and enable the listener in that event again.

Upvotes: 0

Related Questions