不同的 Android 设备在尝试显示浮动小部件时显示错误

问题描述

我正在尝试显示浮动小部件

但是当我在两个小米红米手机上运行应用程序时,我收到了这个错误信息

CreateServiceData{token=android.os.BinderProxy@23c3153 className=com.xyz.x.FloatingViewService packageName=com.xyz.com intent=null}"

所以我在 SO 上搜索并提出了以下解决方案并且它有效

//Add the view to the window.
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,//WindowManager.LayoutParams.TYPE_PHONE,WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,PixelFormat.TRANSLUCENT);

但现在它在我尝试过的其他手机上显示此消息,例如三星和 vivo

android.view.WindowManager$BadTokenException:Unable to add window android.view.ViewRootlmpl$W@25a1f437 -- permission denied for this window type"

如果我这样做就可以了

//Add the view to the window.
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.TYPE_PHONE,//WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,PixelFormat.TRANSLUCENT);

如果我设置了 WindowManager.LayoutParams.TYPE_PHONE,,它可以在三星和 vivo 上运行,天知道在哪些手机上运行,​​但在红米手机上不起作用

如果设置此 WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,,它适用于红米手机,而不适用于其他手机。

任何建议请任何事情 这是完整的代码

        //Inflate the floating view layout we created
        View mFloatingView = LayoutInflater.from(this).inflate(R.layout.layout_floating_widget,null);

        //Add the view to the window.
        final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,//                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,PixelFormat.TRANSLUCENT);

        //Specify the view position
        params.gravity = Gravity.TOP | Gravity.LEFT;        //Initially view will be added to top-left corner
        params.x = 0;
        params.y = 100;

        //Add the view to the window
        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        mWindowManager.addView(mFloatingView,params);

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)