0

Update : Problem is solved.

WARNING : This question Does not have the answer AT ALL which is mentioned above.

The problem is, there should be WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY instead of TYPE_PHONE.

If you are having the same problem I would suggest to ask for permission in java class as shown in the link mentioned by 0X0nosugar in the comments.

I am running a service which creates a transparent floating window with some information and user can drag and drop it anywhere.

So far, after adding all views, my service Crashed and I got this error.

java.lang.RuntimeException:

Unable to create service afm.dragger.Dragger: 

android.view.WindowManager$BadTokenException:

Unable to add window android.view.ViewRootImpl$W@ba63d06 -- permission denied for window type 2002

My minSdkVersion is 21 and targetSdkVersion is 27 and I have sdk 27 on my phone.

Here is my AndroidManifest:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".Dragger"/>
</application>

Here is my Dragger.class:

@Override
    public void onCreate() {
        super.onCreate();

        WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);

        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setBackgroundColor(Color.parseColor("#33FFFFFF"));

        LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        linearLayout.setLayoutParams(linearParams);

        Button button = new Button(this);
        button.setBackground(getResources().getDrawable(R.drawable.button_shape));
        button.setTextColor(getResources().getColor(android.R.color.white));
        button.setTextSize(14);
        button.setText("Stop Service");

        ViewGroup.LayoutParams buttonParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        button.setLayoutParams(buttonParams);

        linearLayout.addView(button, buttonParams);

        final WindowManager.LayoutParams windowParams = new WindowManager.LayoutParams(350, 250, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
        windowParams.x = 0;
        windowParams.y = 0;
        windowParams.gravity = Gravity.START|Gravity.TOP;
        manager.addView(linearLayout, windowParams);

        linearLayout.setOnTouchListener(new View.OnTouchListener() {

            private WindowManager.LayoutParams updatedParams = windowParams;
            int x, y;
            float touchedX, touchedY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        x = updatedParams.x;
                        y = updatedParams.y;

                        touchedX = event.getRawX();
                        touchedY = event.getRawY();

                        break;

                    case MotionEvent.ACTION_MOVE:
                        updatedParams.x = (int) (x + (event.getRawX() - touchedX));
                        updatedParams.y = (int) (y + (event.getRawY() - touchedY));

                        manager.updateViewLayout(linearLayout, updatedParams);

                    default:
                        break;
                }

                return false;
            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                manager.removeView(linearLayout);
                stopSelf();
            }
        });
    }

And finally, here is my MainActivity:

start.setOnClickListener is set to :

startService(new Intent(MainActivity.this, Dragger.class));
Detained Developer
  • 1,134
  • 2
  • 13
  • 33
  • Please share the app Manifest and how you show the floating window – Bö macht Blau Sep 23 '18 at 17:35
  • Possible duplicate of [Android: Unable to add window. Permission denied for this window type](https://stackoverflow.com/questions/32224452/android-unable-to-add-window-permission-denied-for-this-window-type) – Martin Zeitler Sep 23 '18 at 17:44
  • @0X0nosugar , I've updated the question. – Detained Developer Sep 23 '18 at 17:46
  • @MartinZeitler I literally copied and pasted the error on google. And I found **only one and non related link**. The google always gives me a bunch of links but this time I got only and only one unrelated link. – Detained Developer Sep 23 '18 at 17:47
  • @DetainedDeveloper type `2002` is a floating window - and one of the answers there might answer your question, too - despite it is not literally the same error message. – Martin Zeitler Sep 23 '18 at 17:52
  • @MartinZeitler @0X0nosugar I read the post Martin has linked and Android studio gives error that `` **Which Martin is suggesting is Only Used For System Apps**, So my question is definitely not a duplicate at all. – Detained Developer Sep 23 '18 at 17:57
  • 1
    @Detained Developer - maybe [this one](https://stackoverflow.com/questions/7569937/unable-to-add-window-android-view-viewrootw44da9bc0-permission-denied-for-t) will get you one step further, although with target version 27 you may run into other problems as well – Bö macht Blau Sep 23 '18 at 17:57
  • See the docs on [Behavior changes for Oreo](https://developer.android.com/about/versions/oreo/android-8.0-changes#all-aw) – Bö macht Blau Sep 23 '18 at 18:01
  • @Detained Developer - are you really getting an error if you try to use this permission? My Android Studio version (3.1.4) does not seem to mind – Bö macht Blau Sep 23 '18 at 18:09
  • @0X0nosugar the 3rd last comment link solved my problem. I the problem was I had to set `WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY` I was using `TYPE_PHONE`. Thanks for the help ! – Detained Developer Sep 23 '18 at 18:17
  • Please create an answer and accept your answer. This will remove the question from the list of unanswered questions and also make it more helpful for people with a similar problem. – David Wasser Sep 24 '18 at 14:01

0 Answers0