2012-03-17 7 views
5

Sto creando un'app che avvia un servizio quando viene premuto il pulsante "Start" e lo interrompe quando viene premuto il pulsante "Stop". nel servizio, registro un ascoltatore per il sensore ACCELEROMETER in modo da ottenere i valori dell'accelerometro degli assi x, y, z .. ma quando interrompo la mia applicazione e annullo l'ascoltatore dal sensore, anche allora ottengo i miei valori di accelerometro.Come annullare la registrazione di un listener da un sensore dopo l'interruzione del servizio?

Ecco il codice:

// Service 
public class Accel extends Service 
{ 
    private static Context CONTEXT; 
    private static Sensor sensor; 
    private static SensorManager sensorManager; 
    private static boolean running = false; 

    @Override 
    public void onCreate() 
    { 
    } 

    // code to execute when the service is shutting down 
    @Override 
    public void onDestroy() 
    { 
     if (isListening()) 
      stopListening(); 
    } 

    // code to execute when the service is starting up 
    @Override 
    public void onStart(Intent intent, int startid) 
    { 
     CONTEXT = this; 
     startListening(this); 
    } 

    public static Context getContext() 
    { 
     return CONTEXT; 
    } 

    // Returns true if the manager is listening to orientation changes 
    public static boolean isListening() 
    { 
     return running; 
    } 

    //Unregisters listeners 
    public static void stopListening() 
    { 
     running = false; 
     sensorManager.unregisterListener(sensorEventListener, sensor); 
    } 

    /** 
    * Registers a listener and start listening 
    * @param accelerometerListener 
    *    callback for accelerometer events 
    */ 
    public static void startListening(AccelerometerListener accelerometerListener) 
    { 
     sensorManager = (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE); 
     List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER); 
     if (sensors.size() > 0) 
     { 
      sensor = sensors.get(0); 
      running = sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_GAME); 
      listener = accelerometerListener; 
     } 
    } 

    /** 
    * The listener that listen to events from the accelerometer listener 
    */ 
    private static SensorEventListener sensorEventListener = 
    new SensorEventListener() 
    { 
    public void onAccuracyChanged(Sensor sensor, int accuracy) {} 
    public void onSensorChanged(SensorEvent event) 
    { 
       // the code to perform on sensor change 
    } 
}; 

}

Qualcuno può darmi una mano ??

+0

Questo servizio potrebbe dover essere arrestato dall'app – zapl

+0

interrotta dalla mia app. L'attività principale della mia app arresta il servizio chiamando il metodo stopService(). – Nikunj

+0

può vedere che questa domanda è vecchia. Hai trovato una soluzione? :) –

risposta

0

onDestroy() non può essere richiamato dal sistema operativo direttamente sulla tua app. Inserisci la tua chiamata stopListening() in onPause() e startListening() nelle tue funzioni onResume(). In questo modo hai la certezza che l'app si registri e si annulli la registrazione con i sensori.

+3

Dato che sto usando un servizio e non un'attività per questo file java, non posso implementare il metodo onResume() e onPause(). mi puoi suggerire qualsiasi altra opzione. – Nikunj

Problemi correlati