3

Ich versuche Location Update im Hintergrund mit der Batch-Version von Fused Location Provider Api zu bekommen.Android, wie Standort Update in Batch

Ich mache dies, indem ich den Google-Richtlinien folge, indem ich eine Standortaktualisierung mit einem Rückruf für eine Sendung anrufe PendingIntent.

Für die Standortanfrage habe ich setMaxWaitTime verwendet, um die Standortaktualisierungsergebnisse in der Google API-Dokumentation als spezifisch zu erhalten.

Mein Problem ist, dass mein Broadcast-Empfänger nicht den Standort in Batch und immer noch einen Ort nach dem anderen Ortungsdienst erhalten.

Ich habe auch eine neue PendingIntent location update sample project von Google versucht, aber die Standortaktualisierung gibt immer noch nur einen Ort nach dem anderen zurück.

Zur Information habe ich auf einem Nexus 5X-Gerät getestet, wie es eine batching sensor hardware hat.

Fehle ich etwas? Hier

ist ein Beispiel für den Code:

gebaut und angeschlossen Google API-Client

private static final long UPDATE_INTERVAL = 60000; // Every 60 seconds. 
private static final long FASTEST_UPDATE_INTERVAL = 30000; // Every 30 seconds 
private static final long MAX_WAIT_TIME = 60000 * 5; // Batch every 5 minutes. 

public void buildGoogleApiClient() { 
    mGoogleApiClient = new GoogleApiClient.Builder(context) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 

    mLocationRequest = new LocationRequest(); 
    mLocationRequest.setInterval(UPDATE_INTERVAL); 
    mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 
    mLocationRequest.setMaxWaitTime(MAX_WAIT_TIME); 

    mGoogleApiClient.connect(); 
} 

@Override 
public void onConnected(Bundle connectionHint) { 
    try { 
     // Create a pending intent to listening on location service when the update become available 
     Intent mIntent = new Intent(context, LocationReceiver.class); 
     mPendingIntent = PendingIntent.getBroadcast(context, 42, mIntent, PendingIntent.FLAG_CANCEL_CURRENT); 
     // Permission check before launching location services 
     if (ContextCompat.checkSelfPermission(context, 
       Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      return; 
     } 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, mPendingIntent); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

BroadcastReceiver Klasse

public class LocationReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(final Context context, Intent intent) { 
    try { 

     // Check if intent has location result 
     if (LocationResult.hasResult(intent)) { 
      // Get our location from the LocationResult 
      LocationResult locationResult = LocationResult.extractResult(intent); 
      if (locationResult != null) { 

       List<Location> locations = locationResult.getLocations(); 

       for (Location location : locations) { 
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.FRANCE); 
        String dateString = formatter.format(new java.util.Date(location.getTime())); 

        Log.d(TAG, "LOCATION: lat:" + location.getLatitude() + " long:" + location.getLongitude() + 
          " radius:" + location.getAccuracy() + " date:" + dateString); 
       } 

      } else { 
       Log.d(TAG, "Location is null"); 
      } 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

}

Vielen Dank für jede Hilfe.

Antwort

1

Nach dem Testen für ein paar Tage scheint Location Batching mit meinem Code zu arbeiten.

Mir ist aufgefallen, dass ich mit meinem Handy für die Stapelverarbeitung zur Arbeit bewegen muss und es ist zufällig. Irgendwann wird die Standortaktualisierung verzögert und ein Batch ausgeliefert, und manchmal wird nur ein Standort geliefert.

+0

Bitte werfen Sie einen Blick auf diese Metapost https://meta.stackoverflow.com/questions/356451/reminding-people-to-pay-tentity-and-use-skip-when-reviewing –