2010-10-13 3 views
0

Bonjour, il y a un problème intéressant!Lire les capteurs alors que l'écran est noir

Je possède ce 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; 
    } 
}; 

}

Comment vous pouvez voir que je reçois un PARTIAL_WAKE_LOCK et je prends le compte du nombre FFT trasformation le faire par la variable x. Donc, après avoir démarré le service, je noircis l'écran en appuyant sur le bouton rouge du téléphone, et après un moment j'essaie de voir combien de FFT l'ont fait, mais la dernière valeur de x est la même que quand je suis devenu noir l'écran. Après quelques expériences, j'ai découvert que si j'essaye le PARTIAL_WAKE_LOCK, sans passer par la méthode onSensorChanged, ça fonctionne bien, mais il semble que quand l'écran devient noir, je continue à tourner sauf pour l'écouteur du capteur. Comment puis-je réparer cela?

tnks

Valerio

Répondre

Questions connexes