当我们从顶部状态栏启用位置时如何关闭 LocationSettingsRequest 对话框?
How to close LocationSettingsRequest dialog when we enable location from top status bar?
This is my sample code for prompting the user for to enable GPS location.
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());
}
}
If we disable location from status bar setting we are able to get the location settings dialog but when we turn on location (from the top status bar) but still the location dialog appears. I need to dismiss dialog when the location is turned on from status bar settings.
提前致谢!
这在您的代码方面是不可能的。
呼叫:
status.startResolutionForResult(SPActivity.this, REQUEST_CHECK_SETTINGS);
您正在单独打开 Activity
(来自 Google Play 服务库)。此代码应该足够智能,注册 BroadcastReceiver
进行位置更改,然后 return 如果 GPS 已启用,结果到您的 Activity
。
由于您无法更改此代码,我想您需要做的是在 Google 问题跟踪器网站上编写更改请求:https://source.android.com/setup/report-bugs
不可能。该对话框不是您应用程序的一部分。它是 android 系统的一部分,遗憾的是它不提供此功能。
但是,如果您认为您的用户在提示启用位置时可以从状态栏执行此操作,则可以在此系统对话框之前显示您自己的对话框。这样,您就可以控制该对话框。在您的对话框上按确定将带您进入该系统对话框。如果用户要从状态栏打开位置,他们很可能会在您自己的对话中这样做。然后您可以隐藏它并且永远不会显示系统对话框。否则你可以继续前进并展示它。
我不知道我的回答是否适合你,只是意识到用户可能会从状态栏启用位置服务,谢谢你的问题让我想得更多...
我用的定位服务是这样的:
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);
}
}
然后等待 activity 结果再次验证
@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();
}
}
}
希望这符合您的回答
This is my sample code for prompting the user for to enable GPS location.
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());
}
}
If we disable location from status bar setting we are able to get the location settings dialog but when we turn on location (from the top status bar) but still the location dialog appears. I need to dismiss dialog when the location is turned on from status bar settings.
提前致谢!
这在您的代码方面是不可能的。 呼叫:
status.startResolutionForResult(SPActivity.this, REQUEST_CHECK_SETTINGS);
您正在单独打开 Activity
(来自 Google Play 服务库)。此代码应该足够智能,注册 BroadcastReceiver
进行位置更改,然后 return 如果 GPS 已启用,结果到您的 Activity
。
由于您无法更改此代码,我想您需要做的是在 Google 问题跟踪器网站上编写更改请求:https://source.android.com/setup/report-bugs
不可能。该对话框不是您应用程序的一部分。它是 android 系统的一部分,遗憾的是它不提供此功能。
但是,如果您认为您的用户在提示启用位置时可以从状态栏执行此操作,则可以在此系统对话框之前显示您自己的对话框。这样,您就可以控制该对话框。在您的对话框上按确定将带您进入该系统对话框。如果用户要从状态栏打开位置,他们很可能会在您自己的对话中这样做。然后您可以隐藏它并且永远不会显示系统对话框。否则你可以继续前进并展示它。
我不知道我的回答是否适合你,只是意识到用户可能会从状态栏启用位置服务,谢谢你的问题让我想得更多...
我用的定位服务是这样的:
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);
}
}
然后等待 activity 结果再次验证
@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();
}
}
}
希望这符合您的回答