Android 位置设置对话框显示两次或如何检测该对话框显示\关闭
我想在onResume()中每次关闭“前台和位置中的活动”时显示“位置设置”对话框 问题:每次都会创建一个新的设置对话框,而不会破坏前一个对话框,我从对话框中获得堆栈。按“后退”按钮或“取消”按钮将显示以前显示的另一个设置对话框 另一个问题是,当从通知栏启用位置时,对话框仍然显示,我无法关闭它Android 位置设置对话框显示两次或如何检测该对话框显示\关闭,android,google-play-services,locationmanager,Android,Google Play Services,Locationmanager,我想在onResume()中每次关闭“前台和位置中的活动”时显示“位置设置”对话框 问题:每次都会创建一个新的设置对话框,而不会破坏前一个对话框,我从对话框中获得堆栈。按“后退”按钮或“取消”按钮将显示以前显示的另一个设置对话框 另一个问题是,当从通知栏启用位置时,对话框仍然显示,我无法关闭它 public class CheckLocationActivity extends FloatingActionsMenuActivity implements ConnectionCallbacks,
public class CheckLocationActivity extends FloatingActionsMenuActivity implements ConnectionCallbacks, OnConnectionFailedListener {
private GoogleApiClient googleApiClient;
@Override
public void onResume() {
super.onResume();
showDialog();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private void showDialog() {
if (googleApiClient == null) {
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
googleApiClient.connect();
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(30 * 1000);
locationRequest.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
//**************************
// builder.setAlwaysShow(true); //this is the key ingredient
//**************************
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();
final LocationSettingsStates state = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.
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(
CheckLocationActivity.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;
}
}
});
}
}
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}nSettingsStatusCodes.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(CheckLocationActivity.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;
}
}
});
}
}
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
public类CheckLocationActivity扩展了floatingActionsMinualActivity实现了ConnectionCallbacks、OnConnectionFailedListener{
私人GoogleapClient GoogleapClient;
@凌驾
恢复时公开作废(){
super.onResume();
showDialog();
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
私有void showDialog(){
if(googleApiClient==null){
GoogleapClient=新的GoogleapClient.Builder(此)
.addApi(LocationServices.API)
.addConnectionCallbacks(此)
.addOnConnectionFailedListener(this.build();
googleApiClient.connect();
LocationRequest LocationRequest=LocationRequest.create();
locationRequest.setPriority(locationRequest.PRIORITY\u高精度);
位置请求设置间隔(30*1000);
locationRequest.SetFastTestInterval(5*1000);
LocationSettingsRequest.Builder=新建LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
//**************************
//builder.setAlwaysShow(true);//这是关键要素
//**************************
PendingResult=LocationServices.SettingsApi.checkLocationSettings(GoogleAppClient,builder.build());
result.setResultCallback(新的ResultCallback(){
@凌驾
公共void onResult(位置设置结果){
最终状态状态=result.getStatus();
最终位置SettingsStates状态=结果。getLocationSettingsStates();
开关(status.getStatusCode()){
案例位置设置StatusCodes.SUCCESS:
//满足所有位置设置。客户端可以初始化位置
//请求在这里。
打破
案例位置设置StatusCodes.RESOLUTION_要求:
//不满足位置设置。但可以通过显示用户来修复
//对话。
试一试{
//通过调用startResolutionForResult()显示对话框,
//并在onActivityResult()中检查结果。
status.StartResult解决方案(
CheckLocationActivity.this,1000);
}catch(IntentSender.sendtintentexe){
//忽略错误。
}
打破
案例位置设置StatusCodes.SETTINGS\u CHANGE\u不可用:
//位置设置不满意。但是,我们无法修复此问题
//设置,这样我们就不会显示对话框。
打破
}
}
});
}
}
@凌驾
未连接的公共空间(捆绑包){
}
@凌驾
公共空间连接暂停(int i){
}
@凌驾
公共无效onConnectionFailed(ConnectionResult ConnectionResult){
}
}N设置StatusCodes.RESOLUTION_需要:
//不满足位置设置。但是可以通过向用户显示来修复
//对话。
试一试{
//通过调用startResolutionForResult()显示对话框,
//并在onActivityResult()中检查结果。
status.StartResult解决方案(CheckLocationActivity.this,1000);
}catch(IntentSender.sendtintentexe){
//忽略错误。
}
打破
案例位置设置StatusCodes.SETTINGS\u CHANGE\u不可用:
//不满足位置设置。然而,我们没有办法解决这个问题
//设置,这样我们就不会显示对话框。
打破
}
}
});
}
}
@凌驾
未连接的公共空间(捆绑包){
}
@凌驾
公共空间连接暂停(int i){
}
@凌驾
公共无效onConnectionFailed(ConnectionResult ConnectionResult){
}
}
使用布尔值检查对话框是否已显示。请记住在bundle outstate中保留布尔值,并在onCreate中加载,以便在旋转更改时不会丢失该值。使用此解决方案,在第一次创建活动时,您将只显示一次对话框。显示对话框后,布尔值将始终为真。必需:每次onResume呼叫时显示拨号。您解决了问题吗?问题没有解决