0

I'm developing an application that it use the geolocation. The problem is that when the user doesn't close it and the app is in background, it drain a lot of battery and I don't know where is the problem.

My location activity is this:

public abstract class LocationActivity extends FragmentActivity implements LocationListener, GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener {
    /**
     * Variables.
     */
    private     AppGlobal           global;
    private     String              TAG     =   "LOCATIONACTIVITY";
    private     LocationRequest     mLocationRequest;
    private     LocationClient      mLocationClient;
    private     LocationListener    listener;



    /**
     * 
     */
    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(getLayoutResourceId());

        global = ((AppGlobal)getApplicationContext());

        mLocationRequest = LocationRequest.create();
        mLocationRequest.setInterval(1000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setFastestInterval(1000);
        mLocationRequest.setSmallestDisplacement(1);

        mLocationClient = new LocationClient(this, this, this);
        if(GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS)
            mLocationClient.connect();
    }



    /**
     * 
     */
    protected abstract int getLayoutResourceId();



    /**
     * 
     */
    @Override
    public void onStop() {
        Log.v(TAG, "onStop");
        if (mLocationClient.isConnected()) {
            stopPeriodicUpdates();
        }
        mLocationClient.disconnect();
        super.onStop();
    }



    /**
     * 
     */
    @Override
    public void onPause() {
        super.onPause();
        Log.v(TAG, "onPause");
        if (mLocationClient.isConnected()) {
            //mLocationClient.removeLocationUpdates(listener);
            stopPeriodicUpdates();
        }
    }



    /**
     * 
     */
    @Override
    public void onResume() {
        Log.v(TAG, "onResume");
        super.onResume();

        if(mLocationClient.isConnected()) {
            if(listener == null)
                listener = new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    global.latitude = location.getLatitude();
                    global.longitude = location.getLongitude();
                }
            };
            mLocationClient.requestLocationUpdates(mLocationRequest, listener);
        } else
            mLocationClient.connect();
    }



    /**
     * 
     */
    private boolean servicesConnected() {
        Log.v(TAG, "servicesConnected");
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (ConnectionResult.SUCCESS == resultCode) {
            Log.d(LocationUtils.APPTAG, getString(R.string.play_services_available));
            return true;
        } else {
            return false;
        }
    }



    /**
     * 
     */
    public void getLocation(View v) {
        Log.v(TAG, "getLocation");
        if (servicesConnected()) {
            Location currentLocation = mLocationClient.getLastLocation();
            global.latitude = currentLocation.getLatitude();
            global.longitude = currentLocation.getLongitude();
        }
    }



    /**
     * 
     */
    @Override
    public void onConnected(Bundle bundle) {
        Log.v(TAG, "onConnected");
        startPeriodicUpdates();
    }



    /**
     * 
     */
    @Override
    public void onDisconnected() {
        Log.v(TAG, "onDisconnected");
    }



    /**
     * 
     */
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.v(TAG, "onConnectionFailed");
        if (connectionResult.hasResolution()) {
            try {
                connectionResult.startResolutionForResult(this, LocationUtils.CONNECTION_FAILURE_RESOLUTION_REQUEST);
            } catch (IntentSender.SendIntentException e) {
                e.printStackTrace();
            }
        } else {
            //void
        }
    }



    /**
     * 
     */
    @Override
    public void onLocationChanged(Location location) {
        Log.v(TAG, "onLocationChanged");
        global.latitude = location.getLatitude();
        global.longitude = location.getLongitude();
        Log.v(TAG, "lat:" + global.latitude);
        Log.v(TAG, "lon:" + global.longitude);
    }



    /**
     * 
     */
    public void startPeriodicUpdates() {
        Log.v(TAG, "startPeriodicUpdates");
        mLocationClient.requestLocationUpdates(mLocationRequest, this);
    }



    /**
     * 
     */
    private void stopPeriodicUpdates() {
        Log.v(TAG, "stopPeriodicUpdates");
        mLocationClient.removeLocationUpdates(this);
    }


}

I think this should work fine but I'm not sure. Thank you very much for your help.

Yeray
  • 1,265
  • 1
  • 11
  • 23
  • Does your app stop getting location updates when the app is in the background? – TomerZ Oct 29 '14 at 10:15
  • Yes, logcat shows that the onStop method has been called successfully and the location icon in action bar disappears. – Yeray Oct 29 '14 at 10:31

1 Answers1

1

You could try tuning the accuracy of your LocationRequest to PRIORITY_BALANCED_POWER_ACCURACY and set a longer setInterval() and a faster setFasterInterval() to something like 60000 millisecondes (one minute). Right now you're asking your provider to refresh the location every second. If the information is not that sensitive and you don't require such a quick update, it would be a great way to reduce your power consumption.

Also, an app can go in the background in the onPause() method, try disconnecting your client

mLocationClient.disconnect();

I hope this helps!

wheeliez
  • 1,568
  • 1
  • 10
  • 14