2012-12-11 58 views
0

我有以下代碼來顯示一個警告對話框,打開位置服務設置:AlertDialog沒有被駁回

alert = new AlertDialog.Builder(context).create(); 
alert.setTitle("Unable to Retrieve Current Location"); 
alert.setMessage(" Enable Location Service ?"); 
alert.setButton(DialogInterface.BUTTON_POSITIVE,"Open Settings", new DialogInterface.OnClickListener() { 

    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     alert.dismiss(); 
     Intent intent = new Intent(android.provider.Settings.ACTION_SECURITY_SETTINGS); 
     context.startActivity(intent); 

    } 
}); 
alert.setCancelable(false); 
alert.show(); 

的設置正常打開,但是當我按當時沒有駁回了先前的對話框。從設置返回時如何關閉對話框。

+2

在''你以前Activity'通話onResume' 'alert.dismiss();' –

+1

用onClick()中的dialog.dismiss替換alert.dismiss() –

回答

0

試試這個:

AlertDialog.Builder alert = new AlertDialog.Builder (context); 
alert.setTitle ("Unable to Retrieve Current Location"); 
alert.setMessage (" Enable Location Service ?"); 
alert.setButton (DialogInterface.BUTTON_POSITIVE, "Open Settings", 
       new DialogInterface.OnClickListener() { 

    @Override 
    public void onClick(DialogInterface dialog, int which) { 
    Intent intent = new Intent(android.provider.Settings.ACTION_SECURITY_SETTINGS); 
    context.startActivity (intent); 
    }); 

// Create and show the dialog. 
alert.create(); 
alert.show(); 

主要區別是推遲創建直到你添加組件後,調用顯示,而不是要求解僱

0

代替alert.dismiss();

On Click事件

嘗試finish(); //這將完成你的活動

0

使用dialog.dismiss();而不是alert.dismiss();