Wun
Wun

Reputation: 6381

How to return value in OnCompleteListener for Android?

I am trying to get GPS of smartphone like the following code:

In Activity.java , I use the following code to get GPS.

private GPSClass gps;
gps.getGPSLocation();

In GPSClass.java

public void getGPSLocation(){
        FusedLocationProviderClient mLocationClient = LocationServices.getFusedLocationProviderClient(mContext);
        mLocationClient.getLastLocation().addOnCompleteListener(mContext, new OnCompleteListener<Location>() {
            @Override
            public void onComplete(@NonNull Task<Location> task) {
                if(task.isSuccessful()){
                    mLocation = task.getResult();
                    Log.i("getLocation---LOCATION", mLocation.getLatitude() + "/"
                            + mLocation.getLongitude());

                }
            }
        });
    }

But how to return value in GPSClass.java ?

I have try to replace

public void onComplete

instead of

public Location onComplete

But it show the error

'onComplete(Task<Location>)' in 'Anonymous class derived from com.google.android.gms.tasks.OnCompleteListener' clashes with 'onComplete(Task<TResult>)' in 'com.google.android.gms.tasks.OnCompleteListener'; attempting to use incompatible return type

Did I miss something? How to return value in OnCompleteListener? Thanks in advance.

Upvotes: 3

Views: 7167

Answers (2)

Arthur Attout
Arthur Attout

Reputation: 2916

This is because you have a system of callback, and you cannot mix a direct-returning method with a callback.

You seem to want a simple method that directly gives you the location, but as you can see FusedLocationProviderClient.getLastLocation() returns a Task and not a direct value. Because of that, when you call your method using getGPSLocation(), you have to wait for the completion of the task to retrieve the value.

If you want a simple solution, you can directly call the FusedLocationProviderClient in your activity, instead of creating another class :

public class MyActivity extends Activity {

    private FusedLocationProviderClient mLocationClient;

    @Override
    protected void onCreate(){

        mLocationClient = LocationServices.getFusedLocationProviderClient(mContext);
        mLocationClient.getLastLocation().addOnCompleteListener(mContext, new OnCompleteListener<Location>() {
            @Override
            public void onComplete(@NonNull Task<Location> task) {
                if(task.isSuccessful()){
                    handleLocation(task.getResult();
                }
            }
        });
    }

    private void handleLocation(Location location){
        //Do your handling here
    }
}

Upvotes: 0

Murat Karag&#246;z
Murat Karag&#246;z

Reputation: 37584

You could use a callback e.g.

public void getGPSLocation(GPSCallback callback){
        FusedLocationProviderClient mLocationClient = LocationServices.getFusedLocationProviderClient(mContext);
        mLocationClient.getLastLocation().addOnCompleteListener(mContext, new OnCompleteListener<Location>() {
            @Override
            public void onComplete(@NonNull Task<Location> task) {
                if(task.isSuccessful()){
                    mLocation = task.getResult();
                    Log.i("getLocation---LOCATION", mLocation.getLatitude() + "/"
                            + mLocation.getLongitude());
                    callback.onLocation(mLocation);
                }
            }
        });
    }

Create an Interface called GPSCallback e.g.

interface GPSCallback{
void onLocation(Location location);
}

and call the method with the callback

gps.getGPSLocation(new GPSCallback() {
    void onLocation(Location location) {
        // do what you need to do with the location
    }
});

Upvotes: 5

Related Questions