标签:android 摇一摇 sensoreven
今天,在修改闹钟的一个bug,发现了在时钟(android 5.0)设置中有一个摇动行为设置项,主要是针对闹钟响铃声时,如果我们不想起床,可以通过设置摇动手机来关闭铃声或者延时多长时间再响闹铃。
(1)启动一个service,中service中监听摇动手机的操作。
(2)在服务中,注册一个SensorEventListener监听事件,处理Sensor.TYPE_ACCELEROMETER.
当摇动手机,触发摇动事件时,进行相应的事件操作。
这个触发摇动事件的触发值是SENSITIVITY,我们可以调节这个值来调整触发摇动事件的敏感度。
触发摇动手机事件后,进行相对应的操作 handleShakeAction(),我只是弹出了一个Toase,你可以写自己想做的任务操作。
package com.example.shakefunction;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class ShakeService extends Service{
public static final String TAG = "ShakeService";
private SensorManager mSensorManager;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mSensorManager.unregisterListener(mShakeListener);
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
mSensorManager.registerListener(mShakeListener,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
//SensorManager.SENSOR_DELAY_GAME,
50 * 1000); //batch every 50 milliseconds
return super.onStartCommand(intent, flags, startId);
}
private final SensorEventListener mShakeListener = new SensorEventListener() {
private static final float SENSITIVITY = 16;
private static final int BUFFER = 5;
private float[] gravity = new float[3];
private float average = 0;
private int fill = 0;
@Override
public void onAccuracyChanged(Sensor sensor, int acc) {
}
public void onSensorChanged(SensorEvent event) {
final float alpha = 0.8F;
for (int i = 0; i < 3; i++) {
gravity[i] = alpha * gravity[i] + (1 - alpha) * event.values[i];
}
float x = event.values[0] - gravity[0];
float y = event.values[1] - gravity[1];
float z = event.values[2] - gravity[2];
if (fill <= BUFFER) {
average += Math.abs(x) + Math.abs(y) + Math.abs(z);
fill++;
} else {
Log.i(TAG, "average:"+average);
Log.i(TAG, "average / BUFFER:"+(average / BUFFER));
if (average / BUFFER >= SENSITIVITY) {
handleShakeAction();
}
average = 0;
fill = 0;
}
}
};
protected void handleShakeAction() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "shake success", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
Intent intent = new Intent();
intent.setClass(this, ShakeService.class);
if(view == startService){
startService(intent);
}else if(view == stopService){
stopService(intent);
}
<service android:name=".ShakeService"
android:exported="false">
</service>
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:android 摇一摇 sensoreven
原文地址:http://blog.csdn.net/hfreeman2008/article/details/47324043