【问题标题】:How to open Alert DIalog System level in android如何在android中打开Alert DIAlog系统级别
【发布时间】:2014-01-17 17:35:10
【问题描述】:

我有一个应用程序,当蓝牙设备连接/断开连接时会打开警报对话框。 警报对话框由连接蓝牙设备时的 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();

【问题讨论】:

    标签: android


    【解决方案1】:

    这可能对你有帮助...

        @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);
        }
    }
    

    【讨论】:

      【解决方案2】:

      您需要在您的清单中获得SYSTEM_ALERT_WINDOW 权限才能实现这一目标。

      【讨论】:

      • 我已经在 Manifest 文件中有
      猜你喜欢
      • 1970-01-01
      • 2022-01-14
      • 2013-12-27
      • 2017-05-21
      • 1970-01-01
      • 2011-05-16
      • 2012-09-11
      相关资源
      最近更新 更多