1)加速度感应器可获取三个浮点类型分别代表
左右移动加速度 X=values[0]
前后移动加速度 Y=values[1]
垂直方向加速度 Z=values[2]
测试时发现,将手机至于水平桌面稳定后X、Y约为0,Z约为9.5(约等于重力加速度)
2)实现加速度感应器代码如下
- package com.example.androidtest2;
- import android.hardware.Sensor;
- import android.hardware.SensorEvent;
- import android.hardware.SensorEventListener;
- import android.hardware.SensorManager;
- import android.os.Bundle;
- import android.app.Activity;
- import android.util.FloatMath;
- import android.view.Menu;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- private SensorManager mSensorManager=null;
- private Sensor mSensor=null;
- /*摇晃检测阈值,决定了对摇晃的敏感程度,越小越敏感。*/
- private static final double SHAKE_SHRESHOLD = 600;
- /*检测的时间间隔100ms*/
- private static final int UPDATE_INTERVAL = 100;
- /*上次检测的时间*/
- private long lastTime;
- /*上次检测时左右、前后、垂直方向加速度*/
- private float last_X,last_y,last_Z;
- private TextView textView1=null;
- private TextView textView2=null;
- private TextView textView3=null;
- private TextView textView4=null;
- private Button button1=null;
- private Button button2=null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- textView1=(TextView)findViewById(R.id.textView1);
- textView2=(TextView)findViewById(R.id.textView2);
- textView3=(TextView)findViewById(R.id.textView3);
- textView4=(TextView)findViewById(R.id.textView4);
- /*获取系统服务(SENSOR_SERVICE)返回一个SensorManager对象*/
- mSensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
- /*通过SensorManager获取相应的(加速度感应器)Sensor类型对象*/
- mSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
- /*注册相应的SensorService*/
- button1=(Button)findViewById(R.id.button1);
- button1.setOnClickListener(new Button.OnClickListener() {
- @Override
- public void onClick(View arg0) {
- mSensorManager.registerListener(mSensorEventListener, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
- }
- });
- /* 销毁相应的SensorService
- * 很关键的部分,注意,说明文档中提到,即使Activity不可见的时候,感应器依然会继续工作
- * 所以一定要关闭触发器,否则将消耗用户大量电量*/
- button2=(Button)findViewById(R.id.button2);
- button2.setOnClickListener(new Button.OnClickListener() {
- @Override
- public void onClick(View v) {
- mSensorManager.unregisterListener(mSensorEventListener, mSensor);
- }
- });
- }
- /*声明一个SensorEventListener对象用于侦听Sensor事件,并重载onSensorChanged方法*/
- private final SensorEventListener mSensorEventListener=new SensorEventListener() {
- @Override
- public void onSensorChanged(SensorEvent event) {
- if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
- float x=event.values[0];
- float y=event.values[1];
- float z=event.values[2];
- /*显示左右、前后、垂直方向加速度*/
- textView1.setText("左右移动加速度"+String.valueOf(x)+"m/s2");
- textView2.setText("前后移动加速度"+String.valueOf(y)+"m/s2");
- textView3.setText("垂直方向加速度"+String.valueOf(z)+"m/s2");
- /*手机晃动检测*/
- long currentTime=System.currentTimeMillis();
- if(lastTime!=0){
- long diffTime=currentTime-lastTime;
- if(diffTime>UPDATE_INTERVAL){
- float diff_X=x-last_X;
- float diff_Y=y-last_y;
- float diff_Z=z-last_Z;
- float diff=FloatMath.sqrt(diff_X*diff_X+diff_Y*diff_Y+diff_Z*diff_Z)/diffTime*10000;
- if(diff>SHAKE_SHRESHOLD){
- textView4.setText("手机在晃动");
- }else{
- textView4.setText("手机没有晃动");
- }
- }
- }
- lastTime=currentTime;
- last_X=x;
- last_y=y;
- last_Z=z;
- }
- }
- @Override
- public void onAccuracyChanged(Sensor sensor, int accuracy) {
- // TODO Auto-generated method stub
- }
- };
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }