我想在用户来自设置应用时关闭对话框。
这是我的onCreate功能
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
checkLocationService();
}
此功能用户检查位置服务是打开还是关闭。
private void checkLocationService(){
LocationManager lm = (LocationManager)getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch(Exception ex) {
Log.e(TAG,ex.toString());
}
if(!gps_enabled){
showSettingsAlert();
}
}
此功能用于显示警告对话框。
private void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(LoginActivity.this);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing the Settings button.
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent,SETTING_INTENT_REQUEST);
}
});
// On pressing the cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==SETTING_INTENT_REQUEST){
setAlarm();
}
}
答案 0 :(得分:1)
AlertDialog.Builder
实例没有dismiss()方法。因此,而不是AlertDialog.Builder
使用AlertDialog
实例,如下所示:
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
然后你可以使用alertDialog.dismiss();
答案 1 :(得分:0)
据我了解你。您想在回调中取消警报:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==SETTING_INTENT_REQUEST){
if (alert != null && alert.isShowing()) {
alert.dismiss();
}
setAlarm();
}
}
当然,使用它:
AlertDialog alert ;
private void showSettingsAlert() {
alertDialog = new AlertDialog.Builder(LoginActivity.this);
/// setup your builder here
///....
AlertDialog alert = alertDialog.create();