0

I am creating Background Location Tracker using Google FusedLocation API. In requestLocationUpdates(), I am using PendingIntent for background operation. When I run my app, foreground location tracker works fine. However, my IntentService class seems not working. I did not get any notification, nothing happens in background. This is how I am implementing Google FusedLocation API in my Main Fragment Class:

@Override
    public void onConnected(Bundle bundle) {

        .......

        //Starting Background Location Tracker
        if (mRequestingLocationUpdates)
        {
            createLocationRequest();
            startLocationUpdates();
        }
    }



protected void startLocationUpdates() {


        locationIntent = new Intent(getActivity().getApplicationContext(), LocationHandlerService.class);
        locationPendingIntent = PendingIntent.getService(getActivity().getApplicationContext(), 5,
                locationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


        LocationServices.FusedLocationApi.requestLocationUpdates(apiClient, locationRequest, locationPendingIntent);

    }

This is my LocationHandlerService class:

public class LocationHandlerService extends IntentService {

    Location mLocation;
    NotificationManager notificationManager;

    public LocationHandlerService() {
        super("Services.LocationHandlerService");
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        if (LocationResult.hasResult(intent)) {

            LocationResult locationResult = LocationResult.extractResult(intent);
            mLocation = locationResult.getLastLocation();

            if (mLocation != null) {
                setupNotification(mLocation);
            }
        }
    }

    //Setup Notification
    private void setupNotification(Location location) {

        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.location_start_notification)
                .setOngoing(true)
                .setPriority(Notification.PRIORITY_HIGH)
                .setContentTitle(getResources().getString(R.string.location_notification))
                .setContentText("Lat: " + location.getLatitude() + ", Long: " + location.getLongitude());

        notificationManager.notify(0, notification.build());

    }

}

Can anyone tell what wrong with my code???

Vaibhav Agarwal
  • 975
  • 1
  • 7
  • 22
  • Use breakpoints or logging to see if you are getting into `onHandleIntent()`. If you are, perhaps one of your two `if` tests is returning `false`. If not, make sure your service is registered in the manifest. Also, you might consider other tests to confirm that you are actually getting location data (e.g., use something other than a `PendingIntent` first). – CommonsWare Jul 12 '16 at 16:27
  • I have service registered in manifest. Earlier, I was using `LocationListener` instead of `PendingIntent` and it was working fine. I tried debugging using breakpoint; breakpoint got ticked but no data was fetched in the debugger. I tried logging, but unable to find my log in Android Monitor. @CommonsWare – Vaibhav Agarwal Jul 12 '16 at 16:34
  • If I remove `onStartCommand()`, I get `NullPointerException - unable to start service` at `onStartCommand()`. @CommonsWare – Vaibhav Agarwal Jul 12 '16 at 16:36
  • Your `onStartCommand()` is your problem with respect to your question, as you are preventing `onHandleIntent()` from ever being used by the superclass. As to what is `null`, you would need to examine the Java stack trace in detail. – CommonsWare Jul 12 '16 at 17:43

0 Answers0