2017-05-26 43 views
-3

我想從我的班級顯示AlertDialog。程序流程正常,因爲我能夠正確顯示Toast消息。但是,對話框永遠不會顯示?這是爲什麼發生?我沒有收到日誌中的任何錯誤消息和/或沒有發現應用程序崩潰。這可能是導致此行爲的原因。如何顯示簡單的AlertDialog?

我已經給出了所需的權限

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 

請參考我的代碼:

if (serverApkVersion > localApkVersion) { 
    AlertDialog alertDialog = new AlertDialog.Builder(this) 
      .setTitle("Title") 
      .setMessage("Are you sure?") 
      .create(); 
    alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 
    alertDialog.show(); 
    Toast.makeText(context, "toast is working, but dialog is not >:(", Toast.LENGTH_LONG).show(); 
} else { 
    Toast.makeText(context, "up to date!", Toast.LENGTH_LONG).show(); 
} 
+0

您錯過了一行代碼:alertDialog.create(); –

+0

爲了使用'TYPE_SYSTEM_ALERT',因爲MM,用戶必須明確地允許你的應用程序繪製其他應用程序。這意味着您無法啓動運行時權限,並且不足以在清單中添加此權限。您唯一能做的就是從您的設備啓動系統設置頁面,讓用戶授予此權限。 – Opiatefuchs

回答

0

試試這個

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("title"); 
     builder.setMessage("message"); 
     builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       //do your action 
      } 
     });  
     builder.setNegativeButton("No", , new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       //do your action 
      } 
     }); 
     builder.show(); 
+0

謝謝,我感謝你的回答,但你能解釋它與我的不同之處嗎? – user8068857

+0

讓我檢查你的代碼 –

+0

你應該觸發一個點擊事件或一些意圖打開一個對話框box.please顯示你的代碼.. –

0

試試這個簡單的例子:

public static void showAlert(String msg, final Context context) { 
     final AlertDialog.Builder builder = new AlertDialog.Builder(context); 
     builder.setCancelable(false); 
     builder.setTitle("Alert"). 
       setMessage(msg). 
       setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         dialog.dismiss(); 
        } 
       }); 
     AlertDialog dialog = builder.create(); 
     dialog.show(); 

    } 

對於您的代碼,您已獲得用戶的許可。

0

enter image description here只需將您的AlertDialog替換爲AlertDialog.Builder,您就可以輕鬆前往。

if (serverApkVersion > localApkVersion) { 
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(Checking.this) 
        .setTitle("Title") 
        .setMessage("Are you sure?"); 
        alertDialog.show(); 
     Toast.makeText(context, "toast is working, but dialog is not >:(", Toast.LENGTH_LONG).show(); 
    }else { 
     Toast.makeText(context, "up to date!", Toast.LENGTH_LONG).show(); 
    } 
0

您的AlertDialog代碼似乎沒問題。我想這個問題是關於你用來創建instanceAlertDialog.Builder的錯誤Context

用途:

AlertDialog alertDialog = new AlertDialog.Builder(context) 

相反的:

AlertDialog alertDialog = new AlertDialog.Builder(this) 

更新您的代碼如下:

if (serverApkVersion > localApkVersion) { 
    AlertDialog alertDialog = new AlertDialog.Builder(context) 
      .setTitle("Title") 
      .setMessage("Are you sure?") 
      .create(); 
    alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 
    alertDialog.show(); 
    Toast.makeText(context, "toast is working, but dialog is not >:(", Toast.LENGTH_LONG).show(); 
} else { 
    Toast.makeText(context, "up to date!", Toast.LENGTH_LONG).show(); 
} 

僅供參考,如果您的應用在Android 6.0+上運行,那麼您應該爲SYSTEM_ALERT_WINDOW添加運行時權限。詳情請參閱this

希望這會有所幫助〜