当我们从顶部状态栏启用位置时,如何关闭LocationSettingsRequest对话框?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了当我们从顶部状态栏启用位置时,如何关闭LocationSettingsRequest对话框?相关的知识,希望对你有一定的参考价值。
这是我提示用户启用GPS位置的示例代码。
private void showLocationSettingsRequest(Context context) {
try {
/* Initiate Google API Client */
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(SPPlaysetScanActivity.this)
.addApi(LocationServices.API)
.build();
googleApiClient.connect();
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(10000 / 2);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
// Show the dialog by calling startResolutionForResult(), and check the result in onActivityResult().
status.startResolutionForResult(SPActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
Log.d(TAG, "showLocationSettingsRequest :PendingIntent unable to execute request.");
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Log.d(TAG, "showLocationSettingsRequest :Location settings are inadequate,
// and cannot be fixed here. Dialog not created.");
break;
}
}
});
} catch (Exception e) {
Log.d(TAG, "showLocationSettingsRequest EX:" + e.toString());
}
}
如果我们从状态栏设置禁用位置,我们可以获取位置设置对话框,但是当我们打开位置(从顶部状态栏)但仍然出现位置对话框。从状态栏设置打开位置时,我需要关闭对话框。
提前致谢!
这在代码方面是不可能的。呼叫:
status.startResolutionForResult(SPActivity.this, REQUEST_CHECK_SETTINGS);
您正在打开单独的Activity
(来自Google Play服务库)。此代码应该足够智能注册BroadcastReceiver
以进行位置更改,然后如果已启用GPS,则将结果返回到Activity
。
由于您无法更改此代码,我想您需要做的是在Google问题跟踪器网站上写一个更改请求:https://source.android.com/setup/report-bugs
我不知道我的答案是否适合你,只是意识到用户可以从状态栏启用位置服务,谢谢你的问题让我想到更多...
我用于位置服务的是这样的:
private void checkLocationService(){
boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean networkEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!gpsEnabled && !networkEnable) {
Log.e(TAG, "gps service not available");
/*show dialog*/
if (builder == null){
builder = new AlertDialog.Builder(mContext);
builder.setPositiveButton("Enable", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent settingLocation = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(settingLocation, 101);
}
});
builder.setMessage("Please enable location service!");
alertDialog = builder.create();
alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
Log.e(TAG, "cancel tapped");
checkLocationService();
}
});
}
alertDialog.show();
}
else {
if (ActivityCompat.checkSelfPermission(
mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(
mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
Location location = locationManager.getLastKnownLocation(provider);
onLocationChanged(location);
}
}
然后从活动结果等待再次验证
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
//Log.e(TAG, String.valueOf(requestCode) + "|" + String.valueOf(resultCode));
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101){
boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean networkEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (gpsEnabled || networkEnable){
if (mMap != null){
if (mapFragment.getView() != null){
View myLocationBtn = ((View) mapFragment.getView()
.findViewById(Integer.parseInt("1")).getParent())
.findViewById(Integer.parseInt("2"));
myLocationBtn.setClickable(true);
myLocationBtn.performClick();
//myLocationBtn.setVisibility(View.GONE);
/*if (myLocationBtn instanceof ImageView){
myLocationBtn.setBackgroundResource(R.mipmap.ic_launchers);
}*/
Log.e(TAG, "locationButton.performClick()");
}
}
}
else {
checkLocationService();
}
}
}
希望这符合你的答案
不可能。该对话框不属于您的应用。它是android系统的一部分,它不能令人遗憾地提供这种功能。
但是,如果您认为用户在提示启用位置时可以从状态栏执行此操作,则可以在此系统对话框之前显示您自己的对话框。这样,您就可以控制该对话框。在对话框中按“确定”将转到该系统对话框。如果用户要从状态栏打开位置,则很可能他们会在您自己的对话框中执行此操作。然后,您可以隐藏它,并且永远不会显示系统对话框。否则你可以前进并显示它。
以上是关于当我们从顶部状态栏启用位置时,如何关闭LocationSettingsRequest对话框?的主要内容,如果未能解决你的问题,请参考以下文章
在Storyboard中,如何让view从状态栏顶部开始布局
当关闭模态视图控制器时,底层 UIScrollView 调整为全屏