Android AlertDialog始终位于顶部

时间:2017-02-06 00:27:32

标签: android alertdialog

我正在从我的MainActivity触发AlertDialog,它可以正常工作:

public void showCustomAlert(String text){

    final String alertText = text;

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            AlertDialog.Builder myDialogBox = new AlertDialog.Builder(mContext);
            myDialogBox.setTitle("Alert");
            myDialogBox.setMessage(alertText);
            myDialogBox.setCancelable(false);
            myDialogBox.setPositiveButton("OK", new DialogInterface.OnClickListener(){

                  public void onClick(DialogInterface dialog, int whichButton) {
                         dialog.dismiss();
                  }

            });
            myDialogBox.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
            {
                  public void onClick(DialogInterface dialog, int which) {
                         dialog.dismiss();
                  }
            });
            AlertDialog alertDialog = myDialogBox.create();
            alertDialog.show();
       }
    });
}

当我在MainActivity上打开另一个活动并再次触发AlertBox时出现问题:它落后于该活动。当我关闭活动时,它会显示:AlertDialog正在显示。

如何始终将此AlertDialog显示在最顶层?

注意:此AlertDialog由我的MainActivity上的推送通知侦听器触发,而不是通过单击侦听器触发。

2 个答案:

答案 0 :(得分:2)

这是上下文问题,因为对话框使用相同的活动上下文, 如果此对话框必须始终位于Top,则可以使用SYSTEM_ALERT_SERVICE(如

)完成
public void showCustomAlert(String text){

    final String alertText = text;

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            AlertDialog.Builder myDialogBox = new AlertDialog.Builder(mContext);
            myDialogBox.setTitle("Alert");
            myDialogBox.setMessage(alertText);
            myDialogBox.setCancelable(false);
            myDialogBox.setPositiveButton("OK", new DialogInterface.OnClickListener(){

                  public void onClick(DialogInterface dialog, int whichButton) {
                         dialog.dismiss();
                  }

            });
            myDialogBox.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
            {
                  public void onClick(DialogInterface dialog, int which) {
                         dialog.dismiss();
                  }
            });
            AlertDialog alertDialog = myDialogBox.create();
                  alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            alertDialog.show();
       }
    });
}

照顾" android.permission.SYSTEM_ALERT_WINDOW"允许。 希望这可以解决您的问题。

答案 1 :(得分:0)

这对我有用(Kotlin) 注意这个 Utilities.mainactivity 在启动时被初始化为主要活动,以便它可以在这个函数中使用

fun messageBox(message: String, title: String) {
try {
    Utilities.mainActivity.runOnUiThread { //this is so that a nonui thread can show a message
        val dialog = AlertDialog.Builder(Utilities.mainActivity)
        dialog.setTitle(title)
            .setMessage(message)
            .setPositiveButton("Ok", DialogInterface.OnClickListener { dialoginterface, i -> })

        val alertDialog: AlertDialog = dialog.create()
        
        //this is done in an effort to make the dialog always appear on the topmost screen
        alertDialog.window.setType(WindowManager.LayoutParams.LAST_APPLICATION_WINDOW)
        alertDialog.show()
        alertDialog.getWindow().setGravity(Gravity.TOP);
    }
} catch (ex: Exception) {
    Log(ex.toString())
}
}