由于写论文需要,需要用手机加速度采集数据,关于android加速度传感器的介绍网上一抓一大把,但大多都是大同小异,跟官网文档差不多。自己写了个取加速度传感器的APK,发现数据有点不对劲,原理屏幕一关后,系统就自动把各种传感器给停掉了,网上找了很久,发现一些可用的资源。
1、查看手机是否支持锁屏后继续运行传感器
有些手机可以支持后台取传感器数据,有些手机不行,这与硬件厂商具体实现有关,像GPS这些传感器都是很耗电的,厂商也是出于电源管理的考虑,这里是一个不完全列表,总结哪些软件支持屏幕关闭后依然运行传感器。这里更有一款APK(Accelerometer Frequency)专门查看你的手机是否支持。
如果你的手机不支持,那还是不要折腾了,换了设备试试。
2、实现方法
我主要参考了
1)http://blog.kozaxinan.com/2012/08/using-accelerometer-when-screen-off_16.html
2)http://*.com/questions/9982433/android-accelerometer-not-working-when-screen-is-turned-off
星期天我按他们的方法实现了一下,主要思想就是申请锁屏后CPU不休眠:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
wl.acquire();
..
wl.release();
我把service写成activity的一个内部类,而我把申请电源管理的命令放在了service的Oncreate()方法里,之前一直纳闷,明明我的手机检测支持的,锁屏后还是没有取数据,而且连接USB时,锁屏后继续取加速度传感器数据,而拔掉USB后就停了,到处找原因,后来才发现service的OnCreate()方法没执行,坑了我。
丑又丑点,我把电源管理申请放在了Activity的onCreate()方法中,结果可行了,把代码放上来。
public class AccleratePersist extends Activity {
private static final String TAG = "AccleratePersist";
private static final int DELAY = SensorManager.SENSOR_DELAY_NORMAL;
private static boolean STOP = true;// 开始暂停按钮 private SensorManager mSensorManager;
private WakeLock mWakeLock;
private TextView accView;// 显示加速度值
private StringBuilder builder = new StringBuilder();
private long lastTimestamp = 0; private PersistService mySerivece; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
accView = (TextView) this.findViewById(R.id.accValue);
final Button stopButton = (Button) this.findViewById(R.id.stopButton);// 停止测量按钮
final Button clearButton = (Button) this.findViewById(R.id.clear); // 清除数据 View.OnClickListener onClickListener = new View.OnClickListener() { @Override
public void onClick(View view) {
if (view == stopButton) {
STOP = !STOP;
setMeasure();
} else if (view == clearButton) {
builder = new StringBuilder();
accView.setText("");
}
} @SuppressLint("Wakelock")
private void setMeasure() {
if (STOP) {// 停止键按下后,取消监听,是否电源
stopButton.setText("继续");
mSensorManager.unregisterListener(mySerivece);
mWakeLock.release();
} else {
stopButton.setText("停止");
mWakeLock.acquire();// 屏幕熄后,CPU继续运行
mSensorManager
.registerListener(
mySerivece,
mSensorManager
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
DELAY);
}
} };
clearButton.setOnClickListener(onClickListener);
stopButton.setOnClickListener(onClickListener);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mySerivece = new PersistService();
PowerManager manager = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);// CPU保存运行
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);// 屏幕熄掉后依然运行
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(mySerivece.mReceiver, filter);
} class PersistService extends Service implements SensorEventListener { private float[] accValue = new float[3]; public BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) { if (!STOP
&& !intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
return;
}
if (mSensorManager != null) {//取消监听后重写监听,以保持后台运行
mSensorManager.unregisterListener(PersistService.this);
mSensorManager
.registerListener(
PersistService.this,
mSensorManager
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
} } }; public void onAccuracyChanged(Sensor sensor, int accuracy) {
Log.i(TAG, "PersistService.onAccuracyChanged().");
} public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
Log.i(TAG, "PersistService.TYPE_ACCELEROMETER.");
accValue = sensorEvent.values;
for (int i = 0; i < 3; i++) {
builder.append((int) accValue[i]);
builder.append(",");
}
builder.append((sensorEvent.timestamp - lastTimestamp) / 1000000);// 采样时间差
builder.append("\n");
accView.setText(builder.toString());
lastTimestamp = sensorEvent.timestamp;
} }
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
} }
}
记得加一个权限: <uses-permission android:name="android.permission.WAKE_LOCK" />
有问题欢迎交流。