屏幕为黑色时读取传感器

时间:2010-10-13 16:55:21

标签: android listener sensor android-wake-lock

嗨,这里有一个有趣的问题!

我有这项服务:

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方法传递它运行良好,但似乎当屏幕变黑时,除了传感器监听器之外,还要继续运行。我该如何修理?

TNKS

瓦莱里奥

1 个答案:

答案 0 :(得分:0)

这似乎是一个Android Bug

http://code.google.com/p/android/issues/detail?id=3708

似乎只在Android 2.2上部分解决了