关于WindowManager.LayoutParams.TYPE_SYSTEM_ALERT适配的问题

本文探讨了解决在不同安卓系统及设备上使用WindowManager.LayoutParams.TYPE_SYSTEM_ALERT遇到的问题。提出了一种通过启动Dialog样式的Activity来替代Dialog的方法,有效避免了因手机厂商定制系统导致的兼容性难题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于WindowManager.LayoutParams.TYPE_SYSTEM_ALERT适配的问题

需求:在Service中或者全局监听中需要弹出一个Dialog

百度以后发现:代码中设置

alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
功能清单文件中加入

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

OK。搞定,本以为可以收工了,但是......

在小米手机竟然弹不出来,而且没有异常,又是一通百度

找到了解决的办法:需要设置应用 显示悬浮窗口

设置 -> 其他应用管理 -> 相关应用 -> 页面底部的权限管理 -> 勾选显示悬浮窗


相关与这个解决的办法就必须是,在代码中判断MIUI系统,然后调到设置中,还必须的用户自己打开,感觉不太好,先看这个代码

    public static String getSystemProperty(String propName) {  
        String line;  
        BufferedReader input = null;  
        try {  
            Process p = Runtime.getRuntime().exec("getprop " + propName);  
            input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);  
            line = input.readLine();  
            input.close();  
        } catch (IOException ex) {  
            return null;  
        } finally {  
            if (input != null) {  
                try {  
                    input.close();  
                } catch (IOException e) {  
                }  
            }  
        }  
        return line;  
    }  

    public static boolean isMIUIRom(){  
        String property = BaseUtils.getSystemProperty("ro.miui.ui.version.name");  
        return !TextUtils.isEmpty(property);  
    }  

以上是判断MIUI系统的,发现是小米的跳转设置页面即可,至于用户开不开我们控制不了,总体感觉还是不太好


又百度后发现可以改成:WindowManager.LayoutParams.TYPE_TOAST,但是MIUI系统是可以了,但是其他的系统又有问题了

最后绞尽脑汁发现一个比较靠谱的解决方案,启动一个Dialog样式的Activity

1、新建一个Activity 修改extends Activity

2、

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏
        setContentView(R.layout.activity);
    }

3、
<activity android:name=".Activity"
            android:theme="@style/DialogTheme"  />

4、在启动的时候加入Flag
 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 

Activity的布局就用你的Dialog需求布局

综合考虑认为这种方式比较好,不用考虑手机厂商的适配问题,但是你可能需要考虑Activity间的通信问题了


转载:

http://blog.youkuaiyun.com/shadow066/article/details/46342859

package com.example.myapplicationdemo4.listView10; import android.content.Context; import android.graphics.PixelFormat; import android.os.Build; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.WindowManager; import android.widget.TextView; import com.example.myapplicationdemo4.R; public class FloatingWindowManager { private Context context; private WindowManager windowManager; private View floatingView; // 关键:保存悬浮窗View的引用 private WindowManager.LayoutParams params; public FloatingWindowManager(Context context) { this.context = context.getApplicationContext(); windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); } // 创建悬浮窗 public void createFloatingWindow() { WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY : WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, PixelFormat.TRANSLUCENT); params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL; params.x = 0; params.y = 0; // 关键:Inflate布局并保存引用 floatingView = LayoutInflater.from(context).inflate(R.layout.floating_layout, null); // 初始内容 TextView tvContent = floatingView.findViewById(R.id.tvContent); tvContent.setText("初始内容"); windowManager.addView(floatingView, params); } // 更新悬浮窗内容(核心方法) public void updateContent(String newText) { if (floatingView == null) return; // 通过保存的View引用直接修改内容 TextView tvContent = floatingView.findViewById(R.id.tvContent); tvContent.setText(newText); } // 移除悬浮窗 public void removeFloatingWindow() { if (floatingView != null) { windowManager.removeView(floatingView); floatingView = null; } } } 请帮我修改一下这个代码,来实现这个悬浮窗点击穿透到悬浮窗下层的界面
07-10
private void createSecureDisplayContent(int displayId) { DisplayManager dm = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE); Display display = dm.getDisplay(displayId); if (display == null || !display.isValid()) { Log.e(TAG, "Invalid display: " + displayId); return; } try { Context displayContext = createDisplayContext(display); WindowManager wm = (WindowManager) displayContext.getSystemService(Context.WINDOW_SERVICE); View view = new View(displayContext); view.setBackgroundColor(Color.BLACK); // 设置布局参数 WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, // 关键:使用比应用更低的层级 1010,//WindowManager.LayoutParams.TYPE_DISPLAY_OVERLAY // 关键:FLAG_SECURE 允许在安全屏显示,但不响应输入 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_SECURE | WindowManager.LayoutParams.FLAG_SCALED, // 可选 PixelFormat.RGBA_8888 ); // 可选:设置窗口标题(调试用) params.setTitle("InstrumentBackground_" + displayId); // ✅ 5. 使用目标屏幕的 WindowManager 添加视图 wm.addView(view, params); Log.i(TAG, "成功在屏幕 (ID=" + displayId + ") 添加黑屏"); } catch (Exception e) { Log.e(TAG, "无法在 SECURE 屏幕添加视图", e); } }
08-07
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值