4

I am trying to run a location tracker in the background. To do this, i read i need to use pendingIntent with fusedLocationProviderClient, so that my app will continue to query the user's location every x seconds, even if the app is minimized/closed.

i am working with code from this repository https://github.com/googlesamples/android-play-location/tree/master/LocationUpdatesPendingIntent/app/src/main/java/com/google/android/gms/location/sample/locationupdatespendingintent

and my mapActivity is in kotlin. the problem is, i can't seem to get any location updates to fire when i start fusedLocationProviderClient using pendingIntent (it works well if i run it in the main thread using a callback, however, this slows down my phone).

does anyone know how to get fusedLocationProviderClient working in my case with the pendingIntent?

I have tried playing with the string in intent.setAction(), using my package name "com.russ.locationalarm" with ".action.PROCESS_UPDATES" appended at the end. i'm new to intents, so i'm not sure if this is the right string to use. i've also tried switching PendingIntent.GetService() to PendingIntent.getBroadCast(), but neither seems to work.

here is the function where i start up the fusedLocationProviderClient:

private fun startLocationUpdates() {

    // Create the location request to start receiving updates

    mLocationRequest!!.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
    mLocationRequest!!.setInterval(INTERVAL)
    mLocationRequest!!.setFastestInterval(FASTEST_INTERVAL)

    // Create LocationSettingsRequest object using location request
    val builder = LocationSettingsRequest.Builder()
    builder.addLocationRequest(mLocationRequest!!)
    val locationSettingsRequest = builder.build()

    val settingsClient = LocationServices.getSettingsClient(this)
    settingsClient.checkLocationSettings(locationSettingsRequest)

    mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
    // new Google API SDK v11 uses getFusedLocationProviderClient(this)
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return
    }

    // mFusedLocationProviderClient!!.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper())
    println("requesting location updates")
    Utils.setRequestingLocationUpdates(this,true)
    mFusedLocationProviderClient!!.requestLocationUpdates(mLocationRequest, getPendingIntent())
}

and here is where i create my pendingIntent:

private fun getPendingIntent(): PendingIntent {
    // Intent intent = new Intent(this, LocationUpdatesIntentService.class);

    // intent.setAction(LocationUpdatesIntentService.ACTION_PROCESS_UPDATES);
    // return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    println("getting pending intent")
    var intent = Intent(this,LocationUpdatesIntentService::class.java)
    intent.setAction("com.russ.locationalarm.action.PROCESS_UPDATES")

    return PendingIntent.getService(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT)
}

and these are the two methods i use to receive broadcast or services

public void onReceive(Context context, Intent intent) {
    System.out.println("RECEIVED INTENT");
    Log.i("asdf","ASDF ASDF ASDF");
    if (intent != null) {
        System.out.println("INTENT NOT EQUAL TO NULL");
        final String action = intent.getAction();
        if (ACTION_PROCESS_UPDATES.equals(action)) {
            LocationResult result = LocationResult.extractResult(intent);
            if (result != null) {
                List<Location> locations = result.getLocations();
                Utils.setLocationUpdatesResult(context, locations);
                Utils.sendNotification(context, Utils.getLocationResultTitle(context, locations));
                Log.i(TAG, Utils.getLocationUpdatesResult(context));
                System.out.println("ASDF ASDF ASDF ASDF");
            }
        }
    }
}

protected void onHandleIntent(Intent intent) {
    System.out.println("RECEIVED INTENT");

    if (intent != null) {
        final String action = intent.getAction();
        if (ACTION_PROCESS_UPDATES.equals(action)) {
            LocationResult result = LocationResult.extractResult(intent);
            if (result != null) {
                List<Location> locations = result.getLocations();
                Utils.setLocationUpdatesResult(this, locations);
                Utils.sendNotification(this, Utils.getLocationResultTitle(this, locations));
                Log.i(TAG, Utils.getLocationUpdatesResult(this));
            }
        }
    }
}

my expected result is that i will trigger one of the methods either onHandleIntent or onReceive. instead, the console doesn't produce any output after 'getting pending intent' from the getPendingIntent function. mLocationRequest should be querying every 1 or 2 seconds, and it was working earlier with the call to fusedLocationProviderClient using looper and the callback, so i know its not that.

juanjo
  • 3,737
  • 3
  • 39
  • 44
Russell Butler
  • 326
  • 4
  • 15

0 Answers0