简体   繁体   English

棉花糖或更高版本中的窗口类型 2010 的 Android 权限被拒绝

[英]Android permission denied for window type 2010 in Marshmallow or higher

I am try to make Chathead type overly draw via Android Service on my app like bellow image.我试图通过 Android 服务在我的应用程序上过度绘制Chathead类型,如波纹管图像。

在此处输入图片说明

This chat head app works on Android Version 5 or lower versions(kitkat,lollipop etc).But I am trying it in Marshmallow and higher versions,Then get this error.这个聊天头应用程序适用于 Android 版本 5 或更低版本(kitkat、棒棒糖等)。但我正在 Marshmallow 和更高版本中尝试它,然后出现此错误。

 android.view.WindowManager$BadTokenException: Unable to add window
 android.view.ViewRootImpl$W@48f5767 -- permission denied for window type 2010

在此处输入图片说明

Chat head code聊天头代码

Note :I am calling this function from Android Service注意:我从 Android 服务调用这个函数

Permission List:权限列表:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.BIND_DEVICE_ADMIN" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

private WindowManager windowManager;
private ImageView chatHead;
WindowManager.LayoutParams params;

@Override
    public void onCreate() {
        super.onCreate();
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        chatHead = new ImageView(this);
        chatHead.setImageResource(R.drawable.settingsicon);
        params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        params.gravity = Gravity.TOP | Gravity.LEFT;
        params.x = 0;
        params.y = 100;
        windowManager.addView(chatHead, params);

        chatHead.setOnTouchListener(new View.OnTouchListener() {
            private int initialX;
            private int initialY;
            private float initialTouchX;
            private float initialTouchY;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        initialX = params.x;
                        initialY = params.y;
                        initialTouchX = event.getRawX();
                        initialTouchY = event.getRawY();                       
                        return true;
                    case MotionEvent.ACTION_UP:                        
                        return true;
                    case MotionEvent.ACTION_MOVE:
                        params.x = initialX + (int) (event.getRawX() - initialTouchX);
                        params.y = initialY + (int) (event.getRawY() - initialTouchY);
                        windowManager.updateViewLayout(chatHead, params);
                        return true;
                }
                return false;
            }
        }
    }

How to resolve this error on Mashmallow or higher android versions ?如何在 Mashmallow 或更高的 android 版本上解决此错误?

Call this method to ask for permission before showing chat head:在显示聊天头之前调用此方法请求许可:

 public void addOverlay() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!Settings.canDrawOverlays(this)) {
            askedForOverlayPermission = true;
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, OVERLAY_PERMISSION_CODE);
        }
    }
}

after that on Permission result if user allows for permission then only you can show chat head like below:在权限结果之后,如果用户允许权限,则只有您可以显示如下聊天头:

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == OVERLAY_PERMISSION_CODE) {
        askedForOverlayPermission = false;
        if (Settings.canDrawOverlays(this)) {
            // SYSTEM_ALERT_WINDOW permission not granted...
            //Toast.makeText(MyProtector.getContext(), "ACTION_MANAGE_OVERLAY_PERMISSION Permission Granted", Toast.LENGTH_SHORT).show();
            Intent serviceIntent = new Intent(Homepage.this, ChatHeadService.class);
            serviceIntent.putExtra("removeUserId", friendId);
            startService(serviceIntent);

        } else {
             Toast.makeText(MyProtector.getContext(), "ACTION_MANAGE_OVERLAY_PERMISSION Permission Denied", Toast.LENGTH_SHORT).show();
        }
    }

Let me know if it helps you... best of luck.如果它对您有帮助,请告诉我……祝您好运。

Strating from version M (api level 23) Android have the new algorithm of requesting permission in runtime.从版本 M(api level 23)开始,Android 有了运行时请求权限的新算法。 See here: https://developer.android.com/training/permissions/requesting.html请参阅此处: https : //developer.android.com/training/permissions/requesting.html

Overlay with type = TYPE_PHONE is deprecated in Orea(API level 26). Orea 中不推荐使用 type = TYPE_PHONE 的覆盖(API 级别 26)。 Only TYPE_APPLICATION_OVERLAY is supported for non-system apps.非系统应用仅支持 TYPE_APPLICATION_OVERLAY。

it looks like your addView() method is failing.看起来您的 addView() 方法失败了。 I've fixed that issue (in some cases) using WindowManager.LayoutParams.TYPE_PHONE for Android SDK under 8.0 and WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY for 8.0 and above.我已经使用WindowManager.LayoutParams.TYPE_PHONE for Android SDK under 8.0 和WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY for 8.0 及更高版本解决了这个问题(在某些情况下)。 You could add some automatic selection in order to grant major compatibility (depending on the active SDK platform) but since Google Play doesn't allows APKs under 8.0, it's clearly unnecessary.您可以添加一些自动选择以授予主要兼容性(取决于活动的 SDK 平台),但由于 Google Play 不允许 8.0 以下的 APK,因此显然没有必要。 Hope it helps.希望它有帮助。

它帮助了我

WindowManager.LayoutParams params = new WindowManager.LayoutParams(); params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM