Different android devices showing error on trying to show floating widget

105 views Asked by At

I'm trying to show a floating widget

but I was getting this error message when I run the app on two Xiaomi redmi devices

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

so I search on SO and came up with this below solution and it worked

//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);

But now its show this message on other phones I tried, like samsung and vivo

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

and it works fine if I do this

//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);

if I set the WindowManager.LayoutParams.TYPE_PHONE, it work on samsung and vivo and god knows on which phones but it doesn't work on redmi phones

and if set this WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, it works on redmi phones and not on others.

Any suggestion please anything here is full code

        //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);
0

There are 0 answers