0

Ich stelle Mock Location (mit FusedLocationApi) zum Testen meiner Anwendung auf Maps basiert, aber es scheint nicht zu funktionieren.setMockMode funktioniert nicht - FusedLocationApi

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 kehrt Erfolg, aber wenn ich getLastKnownLocation nennen es gibt null und onLocationChanged wird nicht ausgelöst, wenn ich kontinuierliche Updates anfordern.

Fehle ich hier etwas?

+0

Jede Antwort? ..... – mallaudin

Antwort

0

wenn Standort-Updates starten, soll es eine PendingIntent erhalten, die OnHandleIntent implemnts, die den Ort von dem 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()); 
     } 
    } 
} 

und Einstellung des Updates extrahiert:

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

FusedLocationApi.requestLocationUpdates(
          mGoogleApiClient, mLocationRequest, locationPendingIntent); 
+0

Funktioniert nicht. Ich kann diese Methode nicht einmal in 'Fused Provider' finden – mallaudin

Verwandte Themen