2

I'm trying to implement getLastLocation().addOnSuccessListener as documented here: https://developer.android.com/training/location/retrieve-current#last-known but in my case within a JobService which is periodically ran by the JobScheduler.

Here is the meaningful part of my jobService:

public class PeriodicJob extends JobService {
    final String TAG = "BNA.Job";
    private FusedLocationProviderClient fusedLocationClient;


    @Override
    public void onCreate() {
        super.onCreate();
        fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
        Log.i(TAG, "onCreate");

    }
    @Override
    public boolean onStartJob(JobParameters params) {
        Log.d(TAG, "Job started..."); // logs every 15mins

        fusedLocationClient.getLastLocation()
                .addOnSuccessListener(this, new OnSuccessListener<Location>() {
                    @Override
                    public void onSuccess(Location location) {
                        // Got last known location. In some rare situations this can be null.
                        if (location != null) {
                            Log.d(TAG, "got Location: " + location);
                            Log.d(TAG, "Accuracy:" + location.getAccuracy()+", LON:"
                                    +location.getLongitude() + ", LAT:" + location.getLatitude());
                        }
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    (...) // never runs
                });
        return true;
    }

When I run this (it compiles fine), I get: Caused by: java.lang.ClassCastException: (...)PeriodicJob cannot be cast to java.util.concurrent.Executor

I looked at Android app crashes on firebase phone authentication and Can't cast this into Executor in google reCAPTCHA but I still couldnt get it working. Just calling .addOnSuccessListener(new OnSuccessListener<Location>() like the second question's answer suggests, throws no error, but the code is never executed (at least, no log is written).

How can I get the call to onSuccess working?

EDIT: In fact it was working all the time (without the first parameter), I had neglected to detect the case when a null location was returned (which I wasnt expecting). Thanks to Mike M.!

Ethan Arnold
  • 134
  • 1
  • 10
  • `PeriodicJob cannot be cast to java.util.concurrent.Executor` - can you add the stacktrace, showing the class and line in that class where this crash occurs, and the code for those referenced lines – PPartisan May 09 '19 at 09:35
  • 2
    That example looks to be geared toward an implementation in an `Activity`, which is presumably why that `addOnSuccessListener()` call has `this` for the first argument, and, indeed the `Task` returned by `getLastLocation()` has an `addOnSuccessListener()` overload that takes an `Activity` there. However, it also has an overload that takes an `Executor`, which is where it's stumbling in your setup. There is, though, yet another overload that needs neither of those, so you can just remove that first `this` argument. Otherwise, you'll need to provide an `Executor` there. – Mike M. May 09 '19 at 10:12
  • @mike I tried that already and no error is thrown, but also nothing happens. @ ppartisan I will add code later when back at my PC – Ethan Arnold May 09 '19 at 10:34
  • 2
    How exactly are you determining that "nothing happens"? Have you set breakpoints in those callbacks? Or logs, perhaps, first thing? In the given snippet, the only thing you're doing is logging if `onSuccess()` is called with a non-null `Location`. – Mike M. May 09 '19 at 10:41
  • 1
    @MikeM. Thank you so much, you were right, without the Executor parameter actually resulted in everything working, I did have a log entry in the onFailure routine but nothing was logged when the location was empty, and that was the case. After adding a log entry at the beginning of onSuccess I immediately saw it running. +1 for you! – Ethan Arnold May 14 '19 at 06:50

0 Answers0