私は、融合ロケーションプロバイダApiのバッチバージョンでバックグラウンドでロケーションアップデートを取得しようとしています。Androidバッチでロケーションアップデートを取得する方法
ブロードキャストでコールバックを使用してロケーションアップデートをリクエストしてGoogleのガイドラインに従ってください。PendingIntent
ロケーションリクエストでは、私はsetMaxWaitTimeを使用して、Google APIドキュメントによって特定のバッチ処理でロケーション更新の結果を取得しました。
私の問題は、私のブロードキャストレシーバーがロケーションをバッチで取得せず、ロケーションサービスから一度に1つのロケーションを取得したことです。
Googleから新しいPendingIntent location update sample projectも試しましたが、ロケーションの更新は一度に1つの場所しか返されません。
詳細については、Nexus 5Xデバイスでbatching sensor hardwareとテストされています。
何か不足していますか?
ビルドと接続GoogleのAPIクライアント
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クラス
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();
}
}
}任意の助け
ありがとう:ここ
はコードの例です。
このメタポストをご覧くださいhttps://meta.stackoverflow.com/questions/356451/reminding-people-to-pay-attention-and-use-skip-when-reviewing –