Battery drains even with app off screen, could it be Location Services doing it?

Posted by John Jorsett on Stack Overflow See other posts from Stack Overflow or by John Jorsett
Published on 2012-09-30T21:35:32Z Indexed on 2012/09/30 21:37 UTC
Read the original article Hit count: 197

Filed under:

I run my app, which uses GPS and Bluetooth, then hit the back button so it goes off screen. I verified via LogCat that the app's onDestroy was called. OnDestroy removes the location listeners and shuts down my app's Bluetooth service. I look at the phone 8 hours later and half the battery charge has been consumed, and my app was responsible according the phone's Battery Use screen. If I use the phone's Settings menu to Force Stop the app, this doesn't occur. So my question is: do I need to do something more than remove the listeners to stop Location Services from consuming power? That's the only thing I can think of that would be draining the battery to that degree when the app is supposedly dormant.

Here's my onStart() where I turn on the location-related stuff and Bluetooth:

@Override
public void onStart() {
    super.onStart();

    if(D_GEN) Log.d(TAG, "MainActivity onStart, adding location listeners");

    // If BT is not on, request that it be enabled.
    // setupBluetooth() will then be called during onActivityResult
    if (!mBluetoothAdapter.isEnabled()) {
        Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
        // Otherwise, setup the Bluetooth session
    } else {
        if (mBluetoothService == null) 
            setupBluetooth();
    }
    // Define listeners that respond to location updates
    mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, GPS_UPDATE_INTERVAL, 0, this);    
    mLocationManager.addGpsStatusListener(this);
    mLocationManager.addNmeaListener(this);

}

And here's my onDestroy() where I remove them:

public void onDestroy() {
    super.onDestroy();
    if(D_GEN) Log.d(TAG, "MainActivity onDestroy, removing update listeners");
    // Remove the location updates
    if(mLocationManager != null) {
        mLocationManager.removeUpdates(this); 
        mLocationManager.removeGpsStatusListener(this);
        mLocationManager.removeNmeaListener(this);
    }
    if(D_GEN) Log.d(TAG, "MainActivity onDestroy, finished removing update listeners");
    if(D_GEN) Log.d(TAG, "MainActivity onDestroy, stopping Bluetooth");
    stopBluetooth();
    if(D_GEN) Log.d(TAG, "MainActivity onDestroy finished");
}

© Stack Overflow or respective owner

Related posts about android