一、接收位置更新
当应用需要跟踪用户足迹,记录用户在不同时间的位置时,就需要周期的从fused location provider获取位置信息。这时就需要调用requestLocationUpdates()这个函数。这个函数需要在与Google Play Services连接成功后调用。
@Override
public void onConnected(Bundle connectionHint) {
...
if (mRequestingLocationUpdates) {
startLocationUpdates();
}
}
protected void startLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
public abstract PendingResult requestLocationUpdates (GoogleApiClient client, LocationRequest request, LocationListener listener)。
- 参数client:GoogleAPIClient实例,参考Android Google Map学习一。
参数request:位置请求对象,包含位置请求间隔时间、优先级等请求基本参数。
创建对象代码如下:
protected void createLocationRequest() {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
- setInterval():设置位置请求间隔,请求间隔以毫秒为单位,这个间隔是个大致间隔,当有其他应用设置的请求间隔比这个间隔小时,此应用收到的位置更新会加快。位置更新也有可能会比设置的间隔更慢。附:为了使地图应用位置实时更新,建议设置间隔在5000ms。
- setFastestInterval():设置最快请求时间间隔,应用设置的间隔时间会相互影响,通过这个函数这是本应用最快处理位置信息的时间间隔。如果不设置,当更新频率过快时,会影响UI的刷新。
- setPriority():设置请求优先级,Google Play Services根据优先级的设置判断该用哪个位置服务来源。
A. PRIORITY_BALANCED_POWER_ACCURACY:这个参数精度在100m左右,是城市级别的精度范围,一般采用wifi或电话运营商。
B. PRIORITY_HIGH_ACCURACY:这个参数精度最高,一般是采用GPS定位。这个参数需要配合Androidmanifest.xml中定义的ACCESS_FINE_LOCATION权限使用。
C. PRIORITY_LOW_POWER:这个精度在10km左右
D. PRIORITY_NO_POWER:这个参数表明当必要时才接受位置更新,此应用不会主动触发更新,但可以接收其他应用触发的位置更新。
- 参数listener:位置更新监听者(接口),当接收到位置更新时,会触发函数onLocationChanged(Location location),从location中获取更新的位置信息。示例代码如下:
public class MainActivity extends ActionBarActivity implements
ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
...
@Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
updateUI();
}
private void updateUI() {
mLatitudeTextView.setText(String.valueOf(mCurrentLocation.getLatitude()));
mLongitudeTextView.setText(String.valueOf(mCurrentLocation.getLongitude()));
mLastUpdateTimeTextView.setText(mLastUpdateTime);
}
}
二、停止更新
想要停止位置更新,需要调用removeLocationUpdates(),传递GoogleAPIClient对象和LocationListener对象。
@Override
protected void onPause() {
super.onPause();
stopLocationUpdates();
}
protected void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient, this);
}
三、状态还原
当设备配置变化时(横屏切换、语言切换等),Activity会销毁重新创建,这时如果需要保存位置状态等信息,需要在onSaveInstanceState()中设置:
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putBoolean(REQUESTING_LOCATION_UPDATES_KEY,
mRequestingLocationUpdates);
savedInstanceState.putParcelable(LOCATION_KEY, mCurrentLocation);
savedInstanceState.putString(LAST_UPDATED_TIME_STRING_KEY, mLastUpdateTime);
super.onSaveInstanceState(savedInstanceState);
}
当需要还原变化状态时,需要在onCreate函数中还原:
@Override
public void onCreate(Bundle savedInstanceState) {
...
updateValuesFromBundle(savedInstanceState);
}
private void updateValuesFromBundle(Bundle savedInstanceState) {
if (savedInstanceState != null) {
// Update the value of mRequestingLocationUpdates from the Bundle, and
// make sure that the Start Updates and Stop Updates buttons are
// correctly enabled or disabled.
if (savedInstanceState.keySet().contains(REQUESTING_LOCATION_UPDATES_KEY)) {
mRequestingLocationUpdates = savedInstanceState.getBoolean(
REQUESTING_LOCATION_UPDATES_KEY);
setButtonsEnabledState();
}
// Update the value of mCurrentLocation from the Bundle and update the
// UI to show the correct latitude and longitude.
if (savedInstanceState.keySet().contains(LOCATION_KEY)) {
// Since LOCATION_KEY was found in the Bundle, we can be sure that
// mCurrentLocationis not null.
mCurrentLocation = savedInstanceState.getParcelable(LOCATION_KEY);
}
// Update the value of mLastUpdateTime from the Bundle and update the UI.
if (savedInstanceState.keySet().contains(LAST_UPDATED_TIME_STRING_KEY)) {
mLastUpdateTime = savedInstanceState.getString(
LAST_UPDATED_TIME_STRING_KEY);
}
updateUI();
}
}
另状态的保存也可以用SharePerferences。