注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

姑射道人的博客

博客新地址:nixuchen.com

 
 
 

日志

 
 

监听手机震动  

2012-03-30 18:47:56|  分类: android |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
import java.util.List;
 import android.hardware.Sensor;
import android.hardware.SensorEvent;
 import android.hardware.SensorEventListener;
 import android.hardware.SensorManager;
 import android.util.Log;


public class AccelerometerListener implements SensorEventListener {

private SensorManager sensorManager;

private List<Sensor> sensors;

private Sensor sensor;

private long lastUpdate = -1;

private long currentTime = -1;

 private float last_x, last_y, last_z;

private float current_x, current_y, current_z, currenForce;

private static final int FORCE_THRESHOLD = 900;

private final int DATA_X = SensorManager.DATA_X;

private final int DATA_Y = SensorManager.DATA_Y;

private final int DATA_Z = SensorManager.DATA_Z;

 public AccelerometerListener(Activity parent) {

SensorManager sensorService = (SensorManager)

parent.getSystemService(Context.SENSOR_SERVICE);

this.sensorManager = sensorManager;

this.subscriber = subscriber;

this.sensors = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);

if (sensors.size() > 0) {

sensor = sensors.get(0);

}

}

public void start () {

 if (sensor!=null) {

 sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME);

}

}

 public void stop () {

sensorManager.unregisterListener(this);

 }

 public void onAccuracyChanged(Sensor s, int valu) {

 }

public void onSensorChanged(SensorEvent event) {

 if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER || event.values.length < 3)

return;

 currentTime = System.currentTimeMillis();

if ((currentTime - lastUpdate) > 100) {

 long diffTime = (currentTime - lastUpdate);

 lastUpdate = currentTime;


current_x = event.values[DATA_X];
 current_y = event.values[DATA_Y];
current_z = event.values[DATA_Z];

 currenForce = Math.abs(current_x+current_y +current_z - last_x - last_y - last_z) / diffTime * 10000;

 if (currenForce > FORCE_THRESHOLD) {

// Device has been shaken now go on and do something

 // you could now inform the parent activity ...

}

 last_x = current_x;
 last_y = current_y;
 last_z = current_z;

}

}

 }
  评论这张
 
阅读(974)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017