1

According to my understanding, unregisterListener() should do the trick and I should stop getting updates from the sensors. However, this is not working. I've tried moving on to the applications menu, as well as opening another application and turning the screen off. Nothing makes the updates from the sensors stop. My code is as follows: I am trying to unregister the listeners in the onPause() and unregisterSensors() function.

public class MainActivity extends Activity implements SensorEventListener,
    LocationListener {

private Location location;
private int lat, lng;
private LocationManager locationManager;
private String provider;
private SensorManager senSensorManager;
private Sensor senAccelerometer;
private Sensor senGyroscope;
private Sensor senMagneticField;
private float last_x, last_y, last_z;
private float gy_x, gy_y, gy_z;
private float mag_x, mag_y, mag_z;
private Button button;
int toggle = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i("MainActivity", "In the main activity");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    location = locationManager.getLastKnownLocation(provider);

    button = (Button) findViewById(R.id.btnButton1);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Do something in response to button click
            toggle++;
            if ((senSensorManager
                    .getDefaultSensor(Sensor.TYPE_ACCELEROMETER)) != null
                    && (location != null)) {
                if (toggle % 2 == 0) {
                    button.setText("Stop");
                    registerSensors();                  
                }

                else {
                    unregisterSensors();
                }
            }
        }
    });
}

public void unregisterSensors() {
    Log.i("MainActivity", senSensorManager.toString());
    senSensorManager.unregisterListener(this);
    locationManager.removeUpdates(this);
    button.setText("Start");
    Log.i("MainActivity", senSensorManager.toString());
}

public void registerSensors() {
    senAccelerometer = senSensorManager
            .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    senGyroscope = senSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
    senMagneticField = senSensorManager
            .getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    senSensorManager.registerListener(this, senAccelerometer,
            SensorManager.SENSOR_DELAY_NORMAL);
    senSensorManager.registerListener(this, senGyroscope,
            SensorManager.SENSOR_DELAY_NORMAL);
    senSensorManager.registerListener(this, senMagneticField,
            SensorManager.SENSOR_DELAY_NORMAL);
    /* DEBUG!! */onLocationChanged(location);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
    // TODO Auto-generated method stub

}

public void onSensorChanged(SensorEvent sensorEvent) {
    Sensor mySensor = sensorEvent.sensor;
    Context context = getApplicationContext();
    int duration = Toast.LENGTH_SHORT;
    Toast toast;

    if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        last_x = sensorEvent.values[0];
        last_y = sensorEvent.values[1];
        last_z = sensorEvent.values[2];

        context = getApplicationContext();
        CharSequence text1 = (new Date()).toString() + " x:" + last_x
                + " y:" + last_y + " z:" + last_z;
        duration = Toast.LENGTH_SHORT;
        toast = Toast.makeText(context, text1, duration);
        toast.show();
    }
    if (mySensor.getType() == Sensor.TYPE_GYROSCOPE) {
        gy_x = sensorEvent.values[0];
        gy_y = sensorEvent.values[1];
        gy_z = sensorEvent.values[2];

        context = getApplicationContext();
        CharSequence text1 = ("Gyroscope x:" + gy_x + " y:" + gy_y + " z:" + gy_z);
        duration = Toast.LENGTH_SHORT;
        toast = Toast.makeText(context, text1, duration);
        toast.show();
    }

    if (mySensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
        mag_x = sensorEvent.values[0];
        mag_y = sensorEvent.values[1];
        mag_z = sensorEvent.values[2];

        context = getApplicationContext();
        CharSequence text1 = ("Magnetic Field x:" + mag_x + " y:" + mag_y
                + " z:" + mag_z);
        duration = Toast.LENGTH_SHORT;
        toast = Toast.makeText(context, text1, duration);
        toast.show();
    }

}    

protected void onPause() {
    super.onPause();
    senSensorManager.unregisterListener(this);
    locationManager.removeUpdates(this);
}

protected void onResume() {
    super.onResume();
    senSensorManager.registerListener(this, senAccelerometer,
            SensorManager.SENSOR_DELAY_NORMAL);
    senSensorManager.registerListener(this, senGyroscope,
            SensorManager.SENSOR_DELAY_NORMAL);
    senSensorManager.registerListener(this, senMagneticField,
            SensorManager.SENSOR_DELAY_NORMAL);
    locationManager.requestLocationUpdates(provider, 400, 1, this);
}

@Override
public void onLocationChanged(Location location) {
    lat = (int) (location.getLatitude());
    lng = (int) (location.getLongitude());
    float speed = location.getSpeed();
    Context context = getApplicationContext();
    CharSequence text = (new Date()).toString() + " Lat:" + lat + " Long:"
            + lng + " Speed:" + speed;
    int duration = Toast.LENGTH_SHORT;
    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
}

@Override
public void onProviderDisabled(String arg0) {
    Toast.makeText(this, "Disabled provider " + provider,
            Toast.LENGTH_SHORT).show();

}

@Override
public void onProviderEnabled(String arg0) {
    Toast.makeText(this, "Enabled new provider " + provider,
            Toast.LENGTH_SHORT).show();
}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub
}
}
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
user2831859
  • 193
  • 1
  • 1
  • 7

1 Answers1

0

It could be because the application is still running in the background, because it may not be dead yet.

frgnvola
  • 518
  • 3
  • 16