Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I keep receiving this exception, when i try to create a system wide overlay using a WindowManager and the corresponding permission:
Unable to add window android.view.ViewRootImpl$W@6b3c898 -- the specified window type is not valid
My Code (run from my activities onCreate):
if(Build.VERSION.SDK_INT >= 23) {
/** check if we already have permission to draw over other apps */
if (!Settings.canDrawOverlays(this)) {
/** if not construct intent to request permission */
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
/** request permission via start activity for result */
startActivityForResult(intent, CHECK_OVERLAY_PERMISSION_REQUEST_CODE);
if(Build.VERSION.SDK_INT >=23 && Settings.canDrawOverlays(this)) {
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
PixelFormat.TRANSLUCENT
View overlay = ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.overlay_playback_controls, null);
//overlay.setOnClickListener(this);
windowManager.addView(overlay, layoutParams);
And if needed, heres the corresponding xml-layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test!!!"
</LinearLayout>
I have already tried all hints from this post (including the linked one), but still have no idea what the problem is: Unable to add window android.view.ViewRoot$W@44da9bc0 -- permission denied for this window type
–
–
I solved the problem in this way:
layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
I think WindowManager.layoutParams
must have a type.
The reason is when you create LayoutParams its must be instance of WindowManager.LayoutParams, and you must set correct flgas.
Constructor params is not width and height, there are some kind of flags.
Otherwise you should get this exception.
When Activity object created and then activity create new PhoneWindow object which is child class of window.
PhoneWindow create Window Manager. You can get in by calling getWindowManager() from actvity.
Root view was created by WindowManager;
line 599
case WindowManagerGlobal.ADD_INVALID_TYPE:
throw new
WindowManager.InvalidDisplayException(
"Unable to add window " + mWindow
+ " -- the specified window type is not
valid");
This is an example of that Exception
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word_detail_activity);
TextView textView = new TextView(this);
textView.setText("Hello");
textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setBackgroundColor(Color.GRAY);
WindowManager manager = getWindowManager();
manager.addView(textView,new WindowManager.LayoutParams(-1,-1));
This code is running without Exception
final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.format = PixelFormat.TRANSLUCENT;
params.gravity = Gravity.START | Gravity.TOP;
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.packageName = getPackageName();
WindowManager manager = getWindowManager();
manager.addView(textView, params);
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.