ANdroid location updates not firing.

  • Replies:0
Bharani Mani
  • Forum posts: 1

Jul 18, 2017, 10:48:44 PM via Website

Hi,
I have an app that gets location updates in background service. I never get location updates on real phone. I call init() from backgrounservice's onCreate(), onConnected() is successfully called, I see requestLocationUpdates() returns success. But after that I never receive onReceive() in my BroadcastReceiver class. I am calling PendingIntent.getService() instead of getBroadCast() as mine is a background service. GPS is enabled on my real phone. Not sure what else is wrong.

public void init(Context context) {
    try {
        this.context = context;
        geoCoder = new Geocoder(context, Locale.getDefault());
        addressFragments = new ArrayList<String>();


        googleApiClient = new GoogleApiClient.Builder(context)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

        locationRequest = new LocationRequest();
        locationRequest.setInterval(INTERVAL);
        locationRequest.setFastestInterval(FAST_INTERVAL);
        //PRIORITY_BALANCED_POWER_ACCURACY
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        googleApiClient.connect();

    } catch (Exception ex) {
        Log.i("LocationClient", "init(): " + ex.getMessage());
    }

}

public void onConnected(@Nullable Bundle bundle) {
    if (ActivityCompat.checkSelfPermission(this.context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this.context,
            android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    Intent mIntent = new Intent(context, LocationReceiver.class);
    //mPendingIntent = PendingIntent.getBroadcast(context, 1011, mIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    mPendingIntent = PendingIntent.getService(context, 1011, mIntent, PendingIntent.FLAG_CANCEL_CURRENT);


    PendingResult<Status> status = FusedLocationApi.requestLocationUpdates(googleApiClient,
            locationRequest, mPendingIntent);

    Location location = FusedLocationApi.getLastLocation(googleApiClient);

}

public class LocationReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (LocationResult.hasResult(intent)) {
            LocationResult locationResult = LocationResult.extractResult(intent);
            Location location = locationResult.getLastLocation();
            if (location != null) {
                // use the Location
                latitude = location.getLatitude();
                longitude = location.getLongitude();
            }
        }

    }
}

Reply