0

I have developed an application using the Fused location provider. In the onConnected() method, I am requesting for location updates and the application logic will be initiated and onLocationChanged() is called.

Problem : onLocationChanged() method is not called in devices in US . This code works perfectly fine on devices in INDIA but does not work on US. By does not work, I mean that locationClient gets connected but onLocationChanged() is never called.

Code Below:

public class LocationReceiver extends BroadcastReceiver
        implements
        // GooglePlayServicesClient.ConnectionCallbacks,
        // GooglePlayServicesClient.OnConnectionFailedListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, LocationListener {
    // LocationClient locationclient = null;
    GoogleApiClient locationclient = null;
    Context contxt;
    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
    @Override
    public void onReceive(Context context, Intent intent) {
        contxt = context;
        // Log.i("locationreciever", "in location rec");,
        Log.i("fused", "in location rec");

        int resp = GooglePlayServicesUtil
                .isGooglePlayServicesAvailable(context);

        if (resp == ConnectionResult.SUCCESS) {
            // locationclient = new LocationClient(context, this, this);
            locationclient = new GoogleApiClient.Builder(context)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)// (mConnectionCallbacks)
                    .addOnConnectionFailedListener(this)// (mOnConnectionFailedListener)
                    .build();
            locationclient.connect();
        } else {
            Log.i("fused", "loc client Google Play Service Error");
        }
    }

    public void updateTransientLocation(Context context, Location loc) {
        // Log.i("updateTransientLocation", "in fn");

        float lat = (float) loc.getLatitude();
        float lon = (float) loc.getLongitude();
        float acc =  loc.getAccuracy();
        float alt = (float) loc.getAltitude();

        if (lat > 0 && lon > 0) {
            PreferenceForApp prefs = new PreferenceForApp(contxt);
            prefs.setTransientLatitude(lat);
            prefs.setTransientLongitude(lon);
            prefs.setTransientAccuracy(acc);
            prefs.setTransientAltitude(alt);
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.i("fused",
                " onLocationChanged Location Request :"
                        + location.getLatitude() + ","
                        + location.getLongitude() + " acc "
                        + location.getAccuracy()+" alt "+location.getAltitude());
        //TODO wait for some time to get location
        updateTransientLocation(contxt, location);
        if (locationclient != null) {
            if (locationclient.isConnected()) {
                // locationclient.removeLocationUpdates(this);
                LocationServices.FusedLocationApi.removeLocationUpdates(
                        locationclient, this);

                locationclient.disconnect();
            }
        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult arg0) {
        PreferenceForApp prefs = new PreferenceForApp(contxt);
//      if (arg0.hasResolution()) {
//          try {
//              // Start an Activity that tries to resolve the error
//              arg0.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST);
//          } catch (IntentSender.SendIntentException e) {
//              e.printStackTrace();
//          }}else{
        Log.i("fused", "loc client connection failed");
        prefs.setGooglePlayServiceErrorCode(arg0.getErrorCode());
    }
//}

    @Override
    public void onConnected(Bundle arg0) {
        PreferenceForApp prefs = new PreferenceForApp(contxt);
        prefs.setGooglePlayServiceErrorCode(0);
        Log.i("fused", "loc client onConnected");
        LocationRequest locationrequest = new LocationRequest();
        locationrequest
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

//      PRIORITY_BALANCED_POWER_ACCURACY
        // locationclient.requestLocationUpdates(locationrequest, this);
        LocationServices.FusedLocationApi.requestLocationUpdates(
                locationclient, locationrequest, this); // mLocationListener);
    }

    // @Override
    // public void onDisconnected() {
    // Log.i("fused", "loc client disconnected");
    // }

    @Override
    public void onConnectionSuspended(int arg0) {
        Log.i("fused", "loc client onConnectionSuspended");
    }
}

Can anyone help me out with this issue? Is there something I am missing here?

young_08
  • 1,196
  • 2
  • 13
  • 35

3 Answers3

0

Maybe it's the fact that the phone can't connect with the service provider?

try using the gps.

String locationProvider = LocationManager.GPS_PROVIDER;
// Or, use GPS location data:
// String locationProvider = LocationManager.NETWORK_PROVIDER;
Rockernaap
  • 143
  • 1
  • 7
  • how can i use location Manager in FusedLocationApi ,? By using FusedLocation I am already getting location updated by GPS – young_08 Sep 07 '15 at 05:49
0

I think you are using NETWORK_PROVIDER to access/get the location update.

Try to use GPS_PROVIDER or use PASSIVE_PROVIDER to get location from either NETWORK_PROVIDER or GPS_PROVIDER.

Make sure you have permission in manifest to access NETWORK and GPS locations.

0

If you are using FusedLocationProviderApi, you have the option of using the SettingsApi to check if a device has the location settings required by an application. The SettingsApi and optionally provides a location dialog to update the device's location settings if they are found to be inadequate. You can look at an example. Run the sample with the location turned off in Settings, and you should see the dialog. Your app may be failing because it doesn't have sufficient permissions, and the location dialog could help.

Shailen Tuli
  • 13,815
  • 5
  • 40
  • 51