Android如何批量获取位置更新
Android how to get location update in batch
我正在尝试使用 Fused Location Provider Api 的批处理版本在后台获取位置更新。
我是按照 Google 准则执行此操作的,方法是通过广播 PendingIntent 回调请求位置更新。
对于位置请求,我使用 setMaxWaitTime 根据 Google API 文档以批处理方式获取位置更新结果。
我的问题是我的广播接收器没有批量获取位置,仍然从定位服务一次获取一个位置。
我也尝试了 google 的新 PendingIntent location update sample project,但位置更新仍然 return 一次只有一个位置。
有关信息,我在 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();
}
}
广播接收器class
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();
}
}
}
感谢您的帮助。
经过几天的测试,我的代码似乎可以使用位置批处理。
我注意到我必须移动 phone 才能进行批处理,而且它是随机的。有时位置更新会延迟并交付一批,有时它只会交付一个位置。
我正在尝试使用 Fused Location Provider Api 的批处理版本在后台获取位置更新。
我是按照 Google 准则执行此操作的,方法是通过广播 PendingIntent 回调请求位置更新。
对于位置请求,我使用 setMaxWaitTime 根据 Google API 文档以批处理方式获取位置更新结果。
我的问题是我的广播接收器没有批量获取位置,仍然从定位服务一次获取一个位置。
我也尝试了 google 的新 PendingIntent location update sample project,但位置更新仍然 return 一次只有一个位置。
有关信息,我在 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();
}
}
广播接收器class
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();
}
}
}
感谢您的帮助。
经过几天的测试,我的代码似乎可以使用位置批处理。
我注意到我必须移动 phone 才能进行批处理,而且它是随机的。有时位置更新会延迟并交付一批,有时它只会交付一个位置。