Search code examples
androidgeolocationandroid-servicefusedlocationproviderapi

Android fused location provider stop updating in sleep mode


Im using fused location provider in my background service, to get the result i tried using onLocationChanged and also tried using pending intent's broadcast receiver. When im using "PRIORITY_BALANCE_ACCURACY" everything is okay, and location is updating as expected, but when im using "PRIORITY_HIGH_ACCURACY" location just stop updating.... i even tried alarmreceiver but the result is still the same, everytime im using "PRIORITY_HIGH_ACCURACY" location wont updating when in sleep mode... but when i turn the screen on i get location updates...

I really need precise location... even when the screen is off any help would be appreciated....


Solution

  • If it's not too much for you, why not try using google's location services:

    on app.gradle:

    compile 'com.google.android.gms:play-services-location:9.0.2'
    

    and on the activity:

    locationRequest = new LocationRequest()
                            .setFastestInterval(5000)
                            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                            .setSmallestDisplacement(10);
    
    
                    GoogleApiClient.Builder clientBuilder = new GoogleApiClient.Builder(ActivityName.this)
                            .addApi(LocationServices.API)
                            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                                @Override
                                public void onConnected(@Nullable Bundle bundle) {
                                    Log.e(TAG,"API CLIENT CONNECTED");
                                    startLocationUpdates();
                                }
    
                                @Override
                                public void onConnectionSuspended(int i) {
                                    Log.e(TAG,"API CLIENT SUSPENDED");
                                }
                            })
                            .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                                @Override
                                public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                                    Log.e(TAG,"API CLIENT CONNECTION FAILED");
                                }
                            });
    
                    apiClient= clientBuilder.build();
                    apiClient.connect();
    

    and this is the start of location updates after the google client connects:

    void startLocationUpdates(){
        if(apiClient!=null && apiClient.isConnected()) {
            LocationServices.FusedLocationApi.requestLocationUpdates(apiClient, locationRequest, ActivityName.this);
            Log.e(TAG, "Location Updates started");
        }
    }
    

    be also sure the activity implements LocationListener so that you can listen to your location updates:

    @Override
    public void onLocationChanged(Location updatedLocation) {
        Log.e(TAG,"LOCATIONUPDATE: "+updatedLocation.toString());
    }