Android iGoogle PlayServicesAvailable和LocationClient
使用Android iGoogle PlayServicesAvailable和LocationClient,android,google-play-services,Android,Google Play Services,使用GooglePlayServices或LocationClient,是否仍然可以在手机上查找位置服务是否已禁用(System->Settings->Location 我想我来不及回答这个问题了。但是没关系。因此,通过新的APILocationSettingsRequest,我们只需单击一下即可在应用程序中启用位置。 为此,我谨此陈辞;初始化GoogleAppClient GoogleApiClient mGoogleApiClient = new GoogleApiClien
GooglePlayServices
或LocationClient
,是否仍然可以在手机上查找位置服务是否已禁用(System->Settings->Location
我想我来不及回答这个问题了。但是没关系。因此,通过新的APILocationSettingsRequest
,我们只需单击一下即可在应用程序中启用位置。
为此,我谨此陈辞;初始化GoogleAppClient
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
mGoogleApiClient.connect();
然后进行如下初始化的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);
}
现在通过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);
LocationSettingsRequest.Builder=新建LocationSettingsRequest.Builder()
.addLocationRequest(MLLocationRequest);
//**************************
builder.setAlwaysShow(true)//这是关键成分
//**************************
Pendingreult结果=
LocationServices.SettingsApi.checkLocationSettings(mgoogleapClient,builder.build());
result.setResultCallback(locationSettingsResultCallback);
因此,只要您进行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;
}
}
};
ResultCallback位置SettingsResultCallback=new ResultCallback(){
@凌驾
公共void onResult(位置设置结果){
最终状态状态=result.getStatus();
最终位置SettingsStates状态=结果。getLocationSettingsStates();
开关(status.getStatusCode()){
案例位置设置StatusCodes.SUCCESS:
//满足所有位置设置。客户端可以初始化位置
//请求在这里。
//startLocationUpdates();
打破
案例位置设置StatusCodes.RESOLUTION_要求:
//不满足位置设置。但可以通过显示用户来修复
//对话。
试一试{
//通过调用startResolutionForResult()显示对话框,
//并在onActivityResult()中检查结果。
status.StartResult解决方案(
PickupLocationActivity.this,1000);
}catch(IntentSender.sendtintentexe){
//忽略错误。
}
打破
案例位置设置StatusCodes.SETTINGS\u CHANGE\u不可用:
//位置设置不满意。但是,我们无法修复此问题
//设置,这样我们就不会显示对话框。
打破
}
}
};
参考资料: