Android 加速度感应器检测手机摇晃

1)加速度感应器可获取三个浮点类型分别代表

左右移动加速度 X=values[0]

前后移动加速度 Y=values[1]

垂直方向加速度 Z=values[2]

测试时发现,将手机至于水平桌面稳定后X、Y约为0,Z约为9.5(约等于重力加速度)

2)实现加速度感应器代码如下

  1. package com.example.androidtest2;  
  2.   
  3. import android.hardware.Sensor;  
  4. import android.hardware.SensorEvent;  
  5. import android.hardware.SensorEventListener;  
  6. import android.hardware.SensorManager;  
  7. import android.os.Bundle;  
  8. import android.app.Activity;  
  9. import android.util.FloatMath;  
  10. import android.view.Menu;  
  11. import android.view.View;  
  12. import android.widget.Button;  
  13. import android.widget.TextView;  
  14.   
  15. public class MainActivity extends Activity {  
  16.   
  17.     private SensorManager mSensorManager=null;  
  18.     private Sensor mSensor=null;  
  19.     /*摇晃检测阈值,决定了对摇晃的敏感程度,越小越敏感。*/  
  20.     private static final double SHAKE_SHRESHOLD = 600;  
  21.     /*检测的时间间隔100ms*/  
  22.     private static final int UPDATE_INTERVAL = 100;  
  23.     /*上次检测的时间*/  
  24.     private long lastTime;  
  25.     /*上次检测时左右、前后、垂直方向加速度*/  
  26.     private float last_X,last_y,last_Z;  
  27.     private TextView textView1=null;  
  28.     private TextView textView2=null;  
  29.     private TextView textView3=null;  
  30.     private TextView textView4=null;  
  31.     private Button button1=null;  
  32.     private Button button2=null;  
  33.     @Override  
  34.     protected void onCreate(Bundle savedInstanceState) {  
  35.         super.onCreate(savedInstanceState);  
  36.         setContentView(R.layout.activity_main);  
  37.         textView1=(TextView)findViewById(R.id.textView1);  
  38.         textView2=(TextView)findViewById(R.id.textView2);  
  39.         textView3=(TextView)findViewById(R.id.textView3);  
  40.         textView4=(TextView)findViewById(R.id.textView4);  
  41.         /*获取系统服务(SENSOR_SERVICE)返回一个SensorManager对象*/  
  42.         mSensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);  
  43.         /*通过SensorManager获取相应的(加速度感应器)Sensor类型对象*/  
  44.         mSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);  
  45.         /*注册相应的SensorService*/  
  46.         button1=(Button)findViewById(R.id.button1);  
  47.         button1.setOnClickListener(new Button.OnClickListener() {  
  48.               
  49.             @Override  
  50.             public void onClick(View arg0) {  
  51.                 mSensorManager.registerListener(mSensorEventListener, mSensor, SensorManager.SENSOR_DELAY_NORMAL);  
  52.             }  
  53.         });  
  54.         /* 销毁相应的SensorService 
  55.          * 很关键的部分,注意,说明文档中提到,即使Activity不可见的时候,感应器依然会继续工作 
  56.          * 所以一定要关闭触发器,否则将消耗用户大量电量*/  
  57.         button2=(Button)findViewById(R.id.button2);  
  58.         button2.setOnClickListener(new Button.OnClickListener() {  
  59.               
  60.             @Override  
  61.             public void onClick(View v) {  
  62.                 mSensorManager.unregisterListener(mSensorEventListener, mSensor);  
  63.             }  
  64.         });  
  65.     }  
  66.     /*声明一个SensorEventListener对象用于侦听Sensor事件,并重载onSensorChanged方法*/  
  67.     private final SensorEventListener mSensorEventListener=new SensorEventListener() {  
  68.           
  69.         @Override  
  70.         public void onSensorChanged(SensorEvent event) {  
  71.             if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){  
  72.                 float x=event.values[0];  
  73.                 float y=event.values[1];  
  74.                 float z=event.values[2];  
  75.                 /*显示左右、前后、垂直方向加速度*/  
  76.                 textView1.setText("左右移动加速度"+String.valueOf(x)+"m/s2");  
  77.                 textView2.setText("前后移动加速度"+String.valueOf(y)+"m/s2");  
  78.                 textView3.setText("垂直方向加速度"+String.valueOf(z)+"m/s2");  
  79.                 /*手机晃动检测*/  
  80.                 long currentTime=System.currentTimeMillis();  
  81.                 if(lastTime!=0){  
  82.                     long diffTime=currentTime-lastTime;  
  83.                     if(diffTime>UPDATE_INTERVAL){  
  84.                         float diff_X=x-last_X;  
  85.                         float diff_Y=y-last_y;  
  86.                         float diff_Z=z-last_Z;  
  87.                         float diff=FloatMath.sqrt(diff_X*diff_X+diff_Y*diff_Y+diff_Z*diff_Z)/diffTime*10000;  
  88.                         if(diff>SHAKE_SHRESHOLD){  
  89.                             textView4.setText("手机在晃动");  
  90.                         }else{  
  91.                             textView4.setText("手机没有晃动");  
  92.                         }  
  93.                     }  
  94.                 }  
  95.                 lastTime=currentTime;  
  96.                 last_X=x;  
  97.                 last_y=y;  
  98.                 last_Z=z;  
  99.             }  
  100.         }  
  101.           
  102.         @Override  
  103.         public void onAccuracyChanged(Sensor sensor, int accuracy) {  
  104.             // TODO Auto-generated method stub   
  105.               
  106.         }  
  107.     };  
  108.   
  109.     @Override  
  110.     public boolean onCreateOptionsMenu(Menu menu) {  
  111.         // Inflate the menu; this adds items to the action bar if it is present.   
  112.         getMenuInflater().inflate(R.menu.main, menu);  
  113.         return true;  
  114.     }  
  115.   
  116. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值