我正在尝试显示一个浮动小部件
但是当我在两台小米红米设备上运行该应用程序时收到此错误消息
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.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
//WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
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.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
// WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
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);