如何在android中打开Alert DIalog System level

kii*_*ana 6 android

我有一个应用程序,当连接/断开蓝牙设备时打开警报对话框. 警报对话框由连接蓝牙设备的BroadcastReceiver触发.

我想打开一个警告对话框,如果我打开我的应用程序(应用程序A)>长按主页>转到另一个应用程序(应用程序B),蓝牙设备已连接 - >我的应用程序A 警报将显示在顶部应用B.

现在发生的事情是,如果我回到应用程序A,我只能看到对话框

我目前的代码:

    final AlertDialog.Builder dialog = new AlertDialog.Builder(activity,
            AlertDialog.THEME_DEVICE_DEFAULT_DARK);

    ... some setting here

    final AlertDialog alert = dialog.create();
    alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    alert.show();
Run Code Online (Sandbox Code Playgroud)

Gop*_*opi 7

这可能对你有所帮助......

    @Override
    public void onReceive(Context context, Intent intent) {
        final WindowManager manager = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.gravity = Gravity.CENTER;
        layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.alpha = 1.0f;
        layoutParams.packageName = context.getPackageName();
        layoutParams.buttonBrightness = 1f;
        layoutParams.windowAnimations = android.R.style.Animation_Dialog;

        final View view = View.inflate(context.getApplicationContext(),R.layout.test_layout, null);
        Button yesButton = (Button) view.findViewById(R.id.yesButton);
        Button noButton = (Button) view.findViewById(R.id.noButton);
        yesButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                manager.removeView(view);
            }
        });
        noButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                manager.removeView(view);
            }
        });
        manager.addView(view, layoutParams);
    }
}
Run Code Online (Sandbox Code Playgroud)