Using Google Play Services LocationClient in background service

My app is designed to track user’s location periodically and send it to server, Recently I changed my code with Google play services Location API.

I created the locationclient and connected to the service in onStartCommand

  • Android Linear Layout - How to Keep Element At Bottom Of View?
  • Is it possible to show progress bar when download via Retrofit 2 Asynchronous?
  • Androind 2.2 Preview 1 - Using ConstraintsLayout with GuideLine app crashes
  • Animation of android TextView's text size and not the entire TextView
  • How do I generate an android debug.keystore from the command-line?
  • Lollipop's backgroundTint has no effect on a Button
  • public int onStartCommand(Intent intent, int flags, int startId) {
        setUpLocationClientIfNeeded();
        if(!mLocationClient.isConnected() || !mLocationClient.isConnecting())
        mLocationClient.connect();
        return START_STICKY;
    
    }
    

    and in onConnected method, I send a location request,

    @Override
    public void onConnected(Bundle arg0) {
        System.out.println("Connected ...");
        mLocationClient.requestLocationUpdates(REQUEST, this);
    
    }
    

    The REQUEST object is,

     private static final LocationRequest REQUEST = LocationRequest.create()
          .setInterval(5*60*1000)      // 5 minutes
          .setFastestInterval(3*60*1000) // 3 minutes
          .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    

    Now the issue is,

    1. the onLocationChanged method is not getting called at the given interval i.e 5 minutes or the fastest interval 3 minutes. From the log I could see, its getting called only twice or thrice after that its not getting called at all ( I checked after 1 hour).

    What is the issue with my above code?. ( I couldnt see any log for ‘disconnected’ also)

    1. To solve this, I tried to use alarmmanager to call the task periodically. Now how to get a single location update through Locationclient from a broadcastreceiver. (locationclient.getLastlocation() only return last stored location but it is not requesting a new location)

    Related posts:

    Android Resources$NotFoundException: Resource ID #0x7f030027
    Android scrollview not filling parent view
    Android layout: distinguishing an 800x480 large device from a 1024x600 tablet
    how do I get file size of temp file in android?
    GPS Android - get positioning only once
    Android SDK source code
  • Android Studio - Tablet emulator not showing correct resolution
  • Set alpha on ImageView without setAlpha
  • Add search toolbar over google map like in native android app
  • How to localize an Android app in Indonesian language
  • How to create an excel file in android?
  • How to change the check box (tick box) color to white in android XML
  • One Solution collect form web for “Using Google Play Services LocationClient in background service”

    Full source code for a background service available here:

    https://gist.github.com/blackcj/20efe2ac885c7297a676

    Try adding the super call to your onStartCommand.

    /**
     * Keeps the service running even after the app is closed.
     * 
     */
    public int onStartCommand (Intent intent, int flags, int startId)
    {
        super.onStartCommand(intent, flags, startId);
    
        setUpLocationClientIfNeeded();
        if(!mLocationClient.isConnected() || !mLocationClient.isConnecting())
        {
            mLocationClient.connect();
        }
    
        return START_STICKY;
    }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.