2

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....

2 Answers2

0

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());
}
MetaSnarf
  • 5,857
  • 3
  • 25
  • 41
0

if you are using android O ,just make the service foreground

if you are using lower one this is what I do : I am using service for location update FYI while tracking user location when app is in the background or screen is off Android OS limits the app stops the location update .

if you want the app to use location update even when screen is off you should bring the app to front (foreground) after 5 minutes of power off or screen off

iDeveloper
  • 1,699
  • 22
  • 47