有效地使用 Android 位置更新
Using Android location updates efficiently
我刚开始玩android提供的定位服务,我在想下面的代码:
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
//update textview
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
当位置更新时,我打算使用可用的 getSpeed()
方法以速度更新文本视图。
我的问题是,这是否会破坏 phone 的电池寿命?如果是这样,是否有更有效的方法?比如注册一个 BroadcastReceiver
来获取位置更新?我只是在寻找可以有效地最大化电池寿命的方式来使用位置更新。
The Google Play services location APIs are preferred over the Android
framework location APIs (android.location) as a way of adding location
awareness to your app. If you are currently using the Android
framework location APIs, you are strongly encouraged to switch to the
Google Play services location APIs as soon as possible.
来源:Making Your App Location-Aware
我已经使用它并发现以下方法非常有效,尤其是在电池充电与频繁准确地获取位置方面:
1. 使用Fused Location Provider, GoogleApiClient:
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
2.根据要求使用LocationRequest:
mLocationRequest = new LocationRequest();
mLocationRequest.setFastestInterval(120000);
//there are other options associated with this objects
// including types of priority
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
3. 如果 class 实现了 LocationListener
,则设置 LocationServices 使用 this
或定义一个新的:
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
所有组件都在包下
com.google.android.gms
我刚开始玩android提供的定位服务,我在想下面的代码:
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
//update textview
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
当位置更新时,我打算使用可用的 getSpeed()
方法以速度更新文本视图。
我的问题是,这是否会破坏 phone 的电池寿命?如果是这样,是否有更有效的方法?比如注册一个 BroadcastReceiver
来获取位置更新?我只是在寻找可以有效地最大化电池寿命的方式来使用位置更新。
The Google Play services location APIs are preferred over the Android framework location APIs (android.location) as a way of adding location awareness to your app. If you are currently using the Android framework location APIs, you are strongly encouraged to switch to the Google Play services location APIs as soon as possible.
来源:Making Your App Location-Aware
我已经使用它并发现以下方法非常有效,尤其是在电池充电与频繁准确地获取位置方面:
1. 使用Fused Location Provider, GoogleApiClient:
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
2.根据要求使用LocationRequest:
mLocationRequest = new LocationRequest();
mLocationRequest.setFastestInterval(120000);
//there are other options associated with this objects
// including types of priority
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
3. 如果 class 实现了 LocationListener
,则设置 LocationServices 使用 this
或定义一个新的:
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
所有组件都在包下
com.google.android.gms