Bla*_*elt 2 android google-play-services
无论如何都要找到手机上是否禁用了位置服务(System -> Settings -> Location),使用GooglePlayServices或LocationClient?
我想我来不及回答这个问题.但没关系.因此,使用新API,LocationSettingsRequest我们只需点击一下即可在应用中启用位置.所以这样做; 初始化GoogleApiClient
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
mGoogleApiClient.connect();
Run Code Online (Sandbox Code Playgroud)
然后将LocationRequest初始化如下:
LocationRequest mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(Util.UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(Util.FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
}
Run Code Online (Sandbox Code Playgroud)
现在通过LocationSettingsRequest进行位置设置请求.
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest);
//**************************
builder.setAlwaysShow(true); //this is the key ingredient
//**************************
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
result.setResultCallback(locationSettingsResultCallback);
Run Code Online (Sandbox Code Playgroud)
因此,只要您创建了LocationSettingsRequest,它就会弹出如下对话框:
因此,要处理用户选择的任何选项,您必须实现回调以处理每个选项,如下所示:
ResultCallback<LocationSettingsResult> locationSettingsResultCallback = new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.
//startLocationUpdates();
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(
PickupLocationActivity.this, 1000);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
break;
}
}
};
Run Code Online (Sandbox Code Playgroud)
参考: https ://developers.google.com/android/reference/com/google/android/gms/location/SettingsApi
| 归档时间: |
|
| 查看次数: |
318 次 |
| 最近记录: |