屏幕黑时读取传感器
你好,这里有一个有趣的问题!
我有这个服务:
public class SensorService extends Service implements SensorEventListener {
private static final String TAG = "MyService";
private MyServiceBinder myServiceBinder = new MyServiceBinder();
private SensorManager mSensorManager;
...........
private PowerManager.WakeLock wl;
private Handler mPeriodicEventHandler; //Per interrompere la trasformata a tempo
private int PERIODIC_EVENT_TIMEOUT = 5000;
private int x=0,mediacount=0;
@Override
public IBinder onBind(Intent intent) {
return myServiceBinder; // object of the class that implements Service interface.
}
public class MyServiceBinder extends Binder implements IMyService {
public void set(String tr, int cp, int sd) {
......
}
}
@Override
public void onCreate() {
Log.d(TAG, "onCreate");
// Get the Wake Lock
PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK,
"wlTag");
wl.acquire();
// Get the SensorManager
mSensorManager= (SensorManager) getSystemService(SENSOR_SERVICE);
mSensorManager.registerListener(this,
mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0),
sDelay);
recc = new recClass(this);
recc.open();
// To execute every PERIODIC_EVENT_TIMEOUT seconds
mPeriodicEventHandler = new Handler();
mPeriodicEventHandler.postDelayed(doPeriodicTask, PERIODIC_EVENT_TIMEOUT);
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy");
mIsStarted = false;
mPeriodicEventHandler.removeCallbacks(doPeriodicTask);
recc.close();
wl.release();
}
@Override
public void onSensorChanged(SensorEvent event) { // SensorEventListener
Sensor sens = event.sensor;
if ((sens.getType() == Sensor.TYPE_ACCELEROMETER) && mIsStarted){
fft.add((float)Math.sqrt((event.values[0]*event.values[0])+(event.values[1]*event.values[1])+(event.values[2]*event.values[2]))); // Add value to the fft
dataBuffer = fft.calculate();
if (dataBuffer != null){
for (int i=0; i<(fft.camp/2);i++)
recc.addValue(toRec, dataBuffer[i]);
x++;
Toast.makeText(this, "FFT: "+x, Toast.LENGTH_SHORT).show();
mIsStarted = false;
// mIsStarted will be true again after PERIODIC_EVENT_TIMEOUT milliseconds
mPeriodicEventHandler.postDelayed(doPeriodicTask, PERIODIC_EVENT_TIMEOUT);
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
private Runnable doPeriodicTask = new Runnable()
{
public void run()
{
mIsStarted = true;
}
};
}
你如何看到我得到一个 PARTIAL_WAKE_LOCK 并计算它通过 x 变量进行了多少次 FFT 转换。
因此,启动服务后,我按下红色电话按钮将屏幕变黑,过了一会儿我尝试查看它完成了多少次 FFT,但 x 的最后一个值与我变黑屏幕时的值相同。
经过一些实验,我发现如果我尝试 PARTIAL_WAKE_LOCK,而不通过 onSensorChanged 方法,它运行良好,但似乎当屏幕变黑时,除了传感器侦听器之外,一切都会继续运行。我该如何修复这个?
瓦莱里奥
Hi here there is a interesting problem!
I have this service:
public class SensorService extends Service implements SensorEventListener {
private static final String TAG = "MyService";
private MyServiceBinder myServiceBinder = new MyServiceBinder();
private SensorManager mSensorManager;
...........
private PowerManager.WakeLock wl;
private Handler mPeriodicEventHandler; //Per interrompere la trasformata a tempo
private int PERIODIC_EVENT_TIMEOUT = 5000;
private int x=0,mediacount=0;
@Override
public IBinder onBind(Intent intent) {
return myServiceBinder; // object of the class that implements Service interface.
}
public class MyServiceBinder extends Binder implements IMyService {
public void set(String tr, int cp, int sd) {
......
}
}
@Override
public void onCreate() {
Log.d(TAG, "onCreate");
// Get the Wake Lock
PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK,
"wlTag");
wl.acquire();
// Get the SensorManager
mSensorManager= (SensorManager) getSystemService(SENSOR_SERVICE);
mSensorManager.registerListener(this,
mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0),
sDelay);
recc = new recClass(this);
recc.open();
// To execute every PERIODIC_EVENT_TIMEOUT seconds
mPeriodicEventHandler = new Handler();
mPeriodicEventHandler.postDelayed(doPeriodicTask, PERIODIC_EVENT_TIMEOUT);
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy");
mIsStarted = false;
mPeriodicEventHandler.removeCallbacks(doPeriodicTask);
recc.close();
wl.release();
}
@Override
public void onSensorChanged(SensorEvent event) { // SensorEventListener
Sensor sens = event.sensor;
if ((sens.getType() == Sensor.TYPE_ACCELEROMETER) && mIsStarted){
fft.add((float)Math.sqrt((event.values[0]*event.values[0])+(event.values[1]*event.values[1])+(event.values[2]*event.values[2]))); // Add value to the fft
dataBuffer = fft.calculate();
if (dataBuffer != null){
for (int i=0; i<(fft.camp/2);i++)
recc.addValue(toRec, dataBuffer[i]);
x++;
Toast.makeText(this, "FFT: "+x, Toast.LENGTH_SHORT).show();
mIsStarted = false;
// mIsStarted will be true again after PERIODIC_EVENT_TIMEOUT milliseconds
mPeriodicEventHandler.postDelayed(doPeriodicTask, PERIODIC_EVENT_TIMEOUT);
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
private Runnable doPeriodicTask = new Runnable()
{
public void run()
{
mIsStarted = true;
}
};
}
How you can see i get a PARTIAL_WAKE_LOCK and i take count of how many FFT trasformation it do by the x variable.
So after starting the service, i turn black the screen pressing the red phone button, and after a while i try to see how many FFT have it done, but the last value of x is the same of when i turned black the screen.
After some experiment i discovered that if i try the PARTIAL_WAKE_LOCK, without passing from the onSensorChanged method it runs good, but it seems that when the screen go black, everithing continue running except for the sensor listener. How can i repair at this?
Tnks
Valerio
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这似乎是一个 Android 错误
http://code.google.com/ p/android/issues/detail?id=3708
在 android 2.2 上似乎只部分解决
It seems to be a Android Bug
http://code.google.com/p/android/issues/detail?id=3708
It seems only partially resolved on android 2.2