Google Play Services for Android. Location client

2019-01-13 11:19发布

问题:

Our app request update location with LocationClient and IntentService. Location doesn't update at all if user disable wifi in phone settings.

We tried to test app with PRIORITY_HIGH_ACCURACY and location updates when wifi disabled.

The same issue if user moving in the place where no wifi networks available.

I think correct behavior is use other phone sensors (like GPS) for update location if phone can not update location with wifi.

Steps to reproduce:

  1. Start update location with pending intent. With parameters

    LocationRequest request = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
            .setInterval(5 * 60 * 1000)
            .setFastestInterval(60*1000)
            .setSmallestDisplacement(70);
    
  2. Disable wifi in phone settings or go with phone in the country where no wifi networks available.

Tested on Android 4.4, 4.3, 4.1, 4.0, 2.3.3. (Nexus7, Nexus4, Samsung GS2, HTC Wildfire and others)

public class TrackerService extends IntentService {
    private void startUpdateLocation() {
        PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),
            0, new Intent(ACTION_LOCATION_UPDATED), 0);
        LocationRequest request = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
            .setInterval(5 * 60 * 1000)
            .setFastestInterval(60*1000)
            .setSmallestDisplacement(70);

        getLocationClient().requestLocationUpdates(request, pendingIntent);
        Log.d(Utils.getMethodName(), "Location update started");
    }

    private LocationClient getLocationClient() {
        if (mLocationClient != null && mLocationClient.isConnected()) return mLocationClient;
        mLocationClient = new LocationClient(this,
            new GooglePlayServicesClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle bundle) {
                    Log.d(Utils.getMethodName(), "Location client. Connected");
                }

                @Override
                public void onDisconnected() {
                    Log.d(Utils.getMethodName(), "Location client. Disconnected");
                }
            },
            new GooglePlayServicesClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult connectionResult) {
                    throw new IllegalStateException("Failed connection to location manager " + connectionResult.toString());
                }
            }
        );
        mLocationClient.connect();
        try {
            while (mLocationClient.isConnecting()) {
                Thread.sleep(200);
            }
        } catch (InterruptedException e) {
            throw new IllegalStateException("Thread interrupted", e);
        }
        return mLocationClient;
    }
}

I tried to send bug report https://code.google.com/p/android/issues/detail?id=63110

Developers of Android can't help.

Where can I report about bugs in Google Play Services?

Is it a bug in Google Play Services?

What work around could you offer?

We don't want use PRIORITY_HIGH_ACCURACY because it drain phone battery. Our application tracking phone location and it shouldn't depends from wifi settings and wifi networks availability.

回答1:

I found solution.

Google changed documentation for Location Client. Now it says:

public static final int PRIORITY_BALANCED_POWER_ACCURACY

Used with setPriority(int) to request "block" level accuracy.

Block level accuracy is considered to be about 100 meter accuracy. Using a coarse accuracy such as this often consumes less power.

Using a coarse accuracy - I think it means that Location Client doesn't use GPS for update location if priority is PRIORITY_BALANCED_POWER_ACCURACY.

So for using GPS you should use PRIORITY_HIGH_ACCURACY.



回答2:

Let me try to give you one answer to this. Up until a few minutes ago I was having the same problem and was hoping there would be an answer here.

I'll describe my problem and solution:

Problem: I didn't get any location updates without an internet connection while using Google Play services LocationClient. GPS enabled.

My code was stable and working with Internet available, but field tests failed.

Possible root causes: 1) LocationClient sucked 2) location client didn't get any gps updates

Solution: To test this I tried using the google maps app. It complained without an internet connection, so I gave it one. Then I tried to locate myself, but a new screen popped up saying that I had to allow "google apps" to access my location. It said that this would not affect 3rd party apps not from google, which turned out to be complete bullshit. When I enabled "only google apps" to access my location my own app suddenly kicked into action within seconds. Developing for android has been full of these "Google moments".



回答3:

If someone is still wondering,

Google documentation says:

If you are using both NETWORK_PROVIDER and GPS_PROVIDER, then you need to request only the ACCESS_FINE_LOCATION permission, because it includes permission for both providers. Permission for ACCESS_COARSE_LOCATION allows access only to NETWORK_PROVIDER.