简体   繁体   中英

Onshake method : activity should start only once

i am starting a activity with onShake method..each time i shake, the activity is getting started but i want the activity to start only the first time i shake...i have seen the similar question here: Start new activity using onShake method ..but could not get much from it..below is my code

public class ShakeListenerTestActivity extends Activity
{
  private ShakeListener mShaker;

  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);              

    final Vibrator vibe = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);

    mShaker = new ShakeListener(this);
    mShaker.setOnShakeListener(new ShakeListener.OnShakeListener () {
      public void onShake()
      {
   vibe.vibrate(100);

        new AlertDialog.Builder(ShakeListenerTestActivity.this)
          .setPositiveButton(android.R.string.ok, null)
          .setMessage("Do stuff here!!")
          .show();
 }
    });

  @Override
  public void onResume()
  {
    mShaker.resume();
    super.onResume();
  }
  @Override
  public void onPause()
  {
    mShaker.pause();
    super.onPause();

  }  
}

in my shakelistener class i am using unregisterListener in pause() method.

 public void pause() {
        if (mSensorMgr != null) {

          mSensorMgr.unregisterListener(this);
          mSensorMgr = null;
        }
      }

what am i missing here any suggestion is appreciated .

Why don't you you just get rid of the mShaker when it does what it is supposed to do ? After you build AlertDialog , just call mShaker...whatever destroys it or at least mShaker.pause . If this is not the answer, I misunderstood your problem sorry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM