开发者

Read sensors while screen is black

开发者 https://www.devze.com 2023-01-20 10:58 出处:网络
Hi here there is a interesting problem! I have this service: public class SensorService extends Service implements SensorEventListener {

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.WakeLoc开发者_如何学JAVAk 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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消