这是我提示用户启用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());
}
}
如果我们从状态栏设置禁用位置,我们可以获取位置设置对话框,但是当我们打开位置(从顶部状态栏)但仍然会出现位置对话框。当从状态栏设置打开位置时,我需要关闭对话框。
提前致谢!
答案 0 :(得分:1)
不可能。该对话框不属于您的应用。它是android系统的一部分,它不能提供这种能力。
但是,如果您认为用户在提示启用位置时可以从状态栏执行此操作,则可以在此系统对话框之前显示您自己的对话框。这样,您就可以控制该对话框。在对话框中按“确定”将转到该系统对话框。如果用户要从状态栏打开位置,则很可能他们会在您自己的对话框中执行此操作。然后,您可以隐藏它,并且永远不会显示系统对话框。否则你可以前进并展示它。
答案 1 :(得分:1)
我不知道我的答案是否适合您,只是意识到用户可以从状态栏启用位置服务,谢谢您的问题让我想到更多...
我用于定位服务的是这样的:
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();
}
}
}
希望这符合你的答案