Why does FusedLoactionProvider.getLocationAvailability () return null (while it shouldn't)?

To initiate my Google Play Services client, follow these steps:

public class MyApplication  extends Application implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener

    protected synchronized GoogleApiClient buildGoogleApiClient() {
        return new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    }


    /* GoogleApiClient.ConnectionCallbacks */
    @Override
    public void onConnected(Bundle bundle) {
        Log.v(TAG, "Google play services connected.");
        boolean isConnected = mGoogleApiClient.isConnected();    // - this is true
        boolean isLocAvailable = LocationServices.FusedLocationApi.getLocationAvailability(mGoogleApiClient).isLocationAvailable();
        // this causes NullPointerException because getLocationAvailabality() returns null. WHY ????
        .
        .
        .
    }

}

      

The version of the Google Play Services library is Rev.24. Why does this null pointer error occur? Is the Google API client initialized, connected, is everything the same as it should be in the documentation? WiFi connection exists ...

+4


source to share


2 answers


Do you still have this problem? This works well in my app (tested on Moto X 2013) with Google Play service Libary Rev 25



But I got it null

when I tested on the Genymotion emulator, which already has Google Apps installed. I had to open a Google app like Google Maps to update the Google Play service. After that, everything worked fine.

+1


source


According to the documentation: https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderApi.html#getLocationAvailability(com.google.android.gms.common.api.GoogleApiClient)

Note that it is always possible for getLastLocation (GoogleApiClient) to return null, even if this method returns true (for example, the location settings have been disabled between calls).

Thus, there are a number of reasons why you might get null. My suggestion is that you check the permissions and make sure the host permission is allowed, not just check if the GoogleClient is connected and the location is available. You can use the following function.



public static boolean checkAppPermissions(Context context, String... strPermissions) {
    for (String permissions : strPermissions) {
        if (!FrameworkUtils.isStringEmpty(permissions)) {
            int result = ContextCompat.checkSelfPermission(context, permissions);
            if (result == PackageManager.PERMISSION_GRANTED) {
                return true;
            }
        }
    }
    return false;
}

      

You can call it like this

checkAppPermissions(mContext, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION)

      

+1


source







All Articles