0

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

我有这项服务:

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

瓦莱里奥

4

1 回答 1

0

这似乎是一个Android Bug

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

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

于 2010-10-13T20:25:52.393 回答