使用onShake方式开始新的活动 [英] Start new activity using onShake method

查看:109
本文介绍了使用onShake方式开始新的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用onShake方法来启动新的活动。一切都很好,但是当新的活动开始,onShake命令仍然是可能的(当我动摇我的手机5倍,新的活动开始5次)。我应该怎么做摇手机,然后开始新的活动只有一次? 这是我的code。

I'd like to start new activity by using onShake method. Everything is ok but when new activity starts, onShake command is still possible (when i shake my phone 5 times, new activity starts 5 times). What should I do to shake phone and start new activity only once? Here is my code.

public class ACTIVITY extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    SensorManager mSensorManager;

    ShakeEvent mSensorListener;

    mSensorListener = new ShakeEvent();
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mSensorManager.registerListener(mSensorListener,
        mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
        SensorManager.SENSOR_DELAY_UI);


    mSensorListener.setOnShakeListener(new ShakeEvent.OnShakeListener() {

      public void onShake() {
          Intent i = new Intent(Shake.this, NEWACTIVITY.class);
          startActivity(i);
      }
    });
}}

对不起我的英文不好。谢谢你的帮助。

Excuse me for my bad English. Thank you for help.

推荐答案

使用<一个href="http://developer.android.com/reference/android/hardware/SensorManager.html#unregisterListener%28android.hardware.SensorEventListener,%20android.hardware.Sensor%29"相对=nofollow> unregisterListener()在的onPause()

这篇关于使用onShake方式开始新的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆