android的摇一摇是基于android的加速度传感器开发实现的,主要使用到了以下类和接口:
- SensorManager 传感器管理器,可以为传感器注册监听器
- SensorEventListener 监听传感器的动作,在回调函数里响应
使用到了这几个方法:
1.getSystemService(String serviceName) 使用这个方法实例化传感器管理器 这个方法是Activity的
2.registerListener(SensorEventListener listener,Sensor sensor,int samplingPeriodUs)
官网关于这个方法的解释:
public boolean registerListener (SensorEventListener listener, Sensor sensor, int samplingPeriodUs)
Added in API level 3
Registers a SensorEventListener for the given sensor at the given sampling frequency.
The events will be delivered to the provided SensorEventListener as soon as they are available. To reduce the power consumption, applications can use registerListener(SensorEventListener, Sensor, int, int) instead and specify a positive non-zero maximum reporting latency.
In the case of non-wake-up sensors, the events are only delivered while the Application Processor (AP) is not in suspend mode. See isWakeUpSensor() for more details. To ensure delivery of events from non-wake-up sensors even when the screen is OFF, the application registering to the sensor must hold a partial wake-lock to keep the AP awake, otherwise some events might be lost while the AP is asleep. Note that although events might be lost while the AP is asleep, the sensor will still consume power if it is not explicitly deactivated by the application. Applications must unregister their SensorEventListeners in their activity’s onPause() method to avoid consuming power while the device is inactive. See registerListener(SensorEventListener, Sensor, int, int) for more details on hardware FIFO (queueing) capabilities and when some sensor events might be lost.
In the case of wake-up sensors, each event generated by the sensor will cause the AP to wake-up, ensuring that each event can be delivered. Because of this, registering to a wake-up sensor has very significant power implications. Call isWakeUpSensor() to check whether a sensor is a wake-up sensor. See registerListener(SensorEventListener, Sensor, int, int) for information on how to reduce the power impact of registering to wake-up sensors.
Note: Don’t use this method with one-shot trigger sensors such as TYPE_SIGNIFICANT_MOTION. Use requestTriggerSensor(TriggerEventListener, Sensor) instead. Use getReportingMode() to obtain the reporting mode of a given sensor.
Parameters
listener A SensorEventListener object.
sensor The Sensor to register to.
samplingPeriodUs The rate sensor events are delivered at. This is only a hint to the system. Events may be received faster or slower than the specified rate. Usually events are received faster. The value must be one of SENSOR_DELAY_NORMAL, SENSOR_DELAY_UI, SENSOR_DELAY_GAME, or SENSOR_DELAY_FASTEST or, the desired delay between events in microseconds. Specifying the delay in microseconds only works from Android 2.3 (API level 9) onwards. For earlier releases, you must use one of the SENSOR_DELAY_* constants.
Returns
true if the sensor is supported and successfully enabled.
See Also
registerListener(SensorEventListener, Sensor, int, Handler)
unregisterListener(SensorEventListener)
unregisterListener(SensorEventListener, Sensor)
我的解释
方法作用
这个方法会为系统的传感器绑定一个监听器,不同的监听器需要传入不同的参数进行控制,
参数解释
listener
一个事件监听器,这个参数需要自己实现SensorEventListener ,要实现OnSensorChanged(SensorEvent event) 这是回调的方法,当传感器的值改变的时候就被调用(比如摇手机)
Sensor
这个是被监听的传感器,可以使用getDefaultSensor(int SensorType) 根据type值的不同可以设置不同的传感器,
主要有:
加速度传感器:Sensor.TYPE_ACCELEROMETER
方向传感器:Sensor.TYPE_ORIENTATION
磁场传感器:Sensor.TYPE_MAGNETIC_FIELD
温度传感器:Sensor.TYPE_TEMPERATURE
光传感器:Sensor.TYPE_LIGHT
压力传感器:Sensor.TYPE_PRESSURE(指示当地大气压力)
不是所有的手机都带有这些传感器,有的没有
samplingPeriodUs
这个参数指定传感器获取数据的频率,有以下几个值:
SensorManager.SENSOR_DELAY_FASTEST
最快,耗电最大,传感器需求高的才用
SensorManager.SENSOR_DELAY_GAME
适合游戏的,一般使用这个就能达到实时的要求
SensorManager.SENSOR_DELAY_NORMAL
一般的,实时要求不高的时候才用
SensorManager.SENSOR_DELAY_UI
最慢,延迟最大
接下来的代码通过监测手机的加速度来判断手机是否被晃动,当xyz任意一个方向的加速度的值超过四十,就认为被晃动了
package com.exe.liuchenfei.yaoyiyao;
import android.content.Context;
import android.content.res.Configuration;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Vibrator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements SensorEventListener {
//传感器管理器
SensorManager sensorManager;
//震动器
Vibrator vibrator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
vibrator=(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
}
@Override
protected void onStop() {
//取消注册监听器
sensorManager.unregisterListener(this);
super.onStop();
}
@Override
protected void onResume() {
super.onResume();
//为系统的加速度传感器注册监听器
sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_GAME);
}
@Override
public void onSensorChanged(SensorEvent event)
{
float values[]=event.values;
//获取x方向的加速度
float xa=values[0];
//获取y方向上的速度
float ya=values[1];
//获取z方向上的速度
//x方向沿屏幕向左,y沿屏幕向上,z垂直屏幕向里
float za=values[2];
//当检测到某一方向加速度大于40时认为晃动了手机
if (Math.abs(xa)>40||Math.abs(ya)>40||Math.abs(za)>40)
{
vibrator.vibrate(500);
Toast.makeText(this,"晃动了手机",Toast.LENGTH_SHORT).show();
}
}
//传感器精度变化时回调的方法
@Override
public void onAccuracyChanged(Sensor sensor,int accuracy)
{
}
}