0

I am setting mock location (using FusedLocationApi) for testing my application based on Maps but it doesn't seem to work.

LocationServices.FusedLocationApi.setMockMode(mGoogleApiClient, true);


 Location location = new Location("fused");
            // Time is needed to create a valid Location
            long currentTime = System.currentTimeMillis();
            long elapsedTimeNanos = 0;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
                elapsedTimeNanos = SystemClock.elapsedRealtimeNanos();
                location.setElapsedRealtimeNanos(elapsedTimeNanos);
            }
            location.setTime(currentTime);
            // new york 40.7128° N, 74.0059° W
            location.setLatitude(40.7128);
            location.setLongitude(74.0059);
            PendingResult<Status> s =  LocationServices.FusedLocationApi.setMockLocation(mGoogleApiClient, location);
            s.setResultCallback(new ResolvingResultCallbacks<Status>(MainActivity.this, 100) {
                @Override
                public void onSuccess(@NonNull Status status) {
                    if(status.isSuccess()){
                        log("mock success");

                        if (mIsFromContinuousUpdates) {
                            startContinuousUpdates();
                            return;
                        }
                        getLastKnowLocation();
                    }
                }

                @Override
                public void onUnresolvableFailure(@NonNull Status status) {

                }
            });

PendingResult returns success but when I call getLastKnownLocation it returns null and onLocationChanged is not triggered when I request continuous updates.

Am I missing anything here?

mallaudin
  • 4,744
  • 3
  • 36
  • 68

1 Answers1

0

when starting location updates, it should receive an pendingIntent that implemnts OnHandleIntent, which extracts the location from the Intent parameter

public class LocationUpdateActivity extends IntentService implements LocationListener {
private static final String TAG = LocationUpdateActivity .class.getSimpleName();
private Location mCurrentLocation;

public LocationUpdateActivity(String name) {
    super(name);
}
public LocationUpdateActivity() {
    super("LocationUpdateActivity");
}

@Override
protected void onHandleIntent(@Nullable Intent intent) {
    Log.i(TAG, "onHandleIntent");
    if (LocationResult.hasResult(intent)) {
        LocationResult locationResult = LocationResult.extractResult(intent);
        mCurrentLocation = locationResult.getLastLocation();
        if (mCurrentLocation != null) {
            Log.d(TAG, "accuracy: " + mCurrentLocation.getAccuracy() + " lat: " + mCurrentLocation.getLatitude() + " lon: " + mCurrentLocation.getLongitude());
        }
    }
}

And setting the updates:

Intent locationIntent = new Intent(mContext, LocationUpdateActivity.class);
PendingIntent locationPendingIntent = PendingIntent.getService(
                            mContext,
                            0,
                            locationIntent,
                            PendingIntent.FLAG_UPDATE_CURRENT
                    );

FusedLocationApi.requestLocationUpdates(
                            mGoogleApiClient, mLocationRequest, locationPendingIntent);
adizhol
  • 11
  • 3