pingpongboss / StandOut

StandOut lets you easily create floating windows in your Android app.
http://pingpongboss.github.com/StandOut
MIT License
1.24k stars 379 forks source link

Permission denied for this type of window at android 6. #37

Closed ch-muhammad-adil closed 8 years ago

ch-muhammad-adil commented 8 years ago

Hey thank you for this library..

I am facing this issue at Android 6.

android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@480c247 -- permission denied for this window type
07-13 19:09:23.481 15732-15732/com.spoofer W/System.err:     at android.view.ViewRootImpl.setView(ViewRootImpl.java:599)
07-13 19:09:23.481 15732-15732/com.spoofer W/System.err:     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310)
07-13 19:09:23.481 15732-15732/com.spoofer W/System.err:     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
07-13 19:09:23.481 15732-15732/com.spoofer W/System.err:     at com.spoofer.dp_widget.lib.StandOutWindow.show(StandOutWindow.java:1113)
07-13 19:09:23.481 15732-15732/com.spoofer W/System.err:     at com.spoofer.dp_widget.lib.StandOutWindow.onStartCommand(StandOutWindow.java:386)
07-13 19:09:23.481 15732-15732/com.spoofer W/System.err:     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3028)
07-13 19:09:23.481 15732-15732/com.spoofer W/System.err:     at android.app.ActivityThread.access$2200(ActivityThread.java:157)
ch-muhammad-adil commented 8 years ago

So I have managed to solve this issue. here is my solution ...

We need to get permissions for this app to draw on system window so here I used this code

if (Build.VERSION.SDK_INT >= 23) {
            checkDrawOverlayPermission();
        }else{
            StandOutWindow.closeAll(this, Dpad.class);
            StandOutWindow.show(this, Dpad.class, StandOutWindow.DEFAULT_ID);
            finish();
        }

here is the rest work I have done.

get the permissions from the user

`/**
     * code to post/handler request for permission
     */
    public final static int REQUEST_CODE = 5463&0xffffff00;

    public void checkDrawOverlayPermission() {
        /** check if we already  have permission to draw over other apps */
        if (!Settings.canDrawOverlays(getApplicationContext())) {
            /** 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, REQUEST_CODE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        /** check if received result code
         is equal our requested code for draw permission  */
        if (requestCode == REQUEST_CODE) {
//            / ** if so check once again if we have permission */
            if (Settings.canDrawOverlays(this)) {
                StandOutWindow.closeAll(this, Dpad.class);
                StandOutWindow.show(this, Dpad.class, StandOutWindow.DEFAULT_ID);
                finish();
            }
        }
    }`