当前位置:首页 > 开发 > 移动开发 > 正文

Android的SensorEventListener例子(摇晃手机)

发表于: 2012-03-09   作者:a2012s   来源:转载   浏览:
摘要: package enent.jj; import android.app.Activity; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener;
package enent.jj;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.Toast;

public class Evnet1Activity extends Activity {
    //检测摇动相关变量
  
   
      private static final int FORCE_THRESHOLD = 350;
  private static final int TIME_THRESHOLD = 100;
  private static final int SHAKE_TIMEOUT = 500;
  private static final int SHAKE_DURATION = 1000;
  private static final int SHAKE_COUNT = 3;
  private int mShakeCount = 0;
  private long mLastShake;
  private long mLastForce;
  private long mLastTime;
  private float mLastX=-1.0f, mLastY=-1.0f, mLastZ=-1.0f;
    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
           setContentView(R.layout.main);

    

       
         
        // 获取传感器管理器
       SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
       // 获取加速度传感器
            Sensor acceleromererSensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

       // 定义传感器事件监听器
        SensorEventListener acceleromererListener = new SensorEventListener() {

           @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
                //什么也不干
            }

            //传感器数据变动事件
            @Override
            public void onSensorChanged(SensorEvent event) {  

               
                    //获取加速度传感器的三个参数
                    float x = event.values[SensorManager.DATA_X];
                    float y = event.values[SensorManager.DATA_Y];
                    float z = event.values[SensorManager.DATA_Z];

                    long now = System.currentTimeMillis();

                if ((now - mLastForce) > SHAKE_TIMEOUT) {
                  mShakeCount = 0;
                }

                if ((now - mLastTime) > TIME_THRESHOLD) {
                  long diff = now - mLastTime;
                  float speed = Math.abs(event.values[SensorManager.DATA_X] + event.values[SensorManager.DATA_Y] + event.values[SensorManager.DATA_Z] - mLastX - mLastY - mLastZ) / diff * 10000;
                  if (speed > FORCE_THRESHOLD) {
                    if ((++mShakeCount >= SHAKE_COUNT) && (now - mLastShake > SHAKE_DURATION)) {
                      mLastShake = now;
                      mShakeCount = 0;
                      if (true) {
                      startRecord();
                      }
                    }
                    mLastForce = now;
                  }
                  mLastTime = now;
                  mLastX = event.values[SensorManager.DATA_X];
                  mLastY = event.values[SensorManager.DATA_Y];
                  mLastZ = event.values[SensorManager.DATA_Z];
                }
                   
                   
                   
               
            }

        };

        //在传感器管理器中注册监听器
        sm.registerListener(acceleromererListener, acceleromererSensor, SensorManager.SENSOR_DELAY_NORMAL);

   }

    // 开始
    public void startRecord() {
    Toast.makeText(getApplicationContext(), "yao",1).show();
    }

 
}

Android的SensorEventListener例子(摇晃手机)

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号