Android permission denied for window type 2010 in Marshmallow or higher

荒凉一梦 提交于 2019-12-01 21:21:15

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. See here: https://developer.android.com/training/permissions/requesting.html

Overlay with type = TYPE_PHONE is deprecated in Orea(API level 26). Only TYPE_APPLICATION_OVERLAY is supported for non-system apps.

it looks like your addView() method is failing. 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. 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. Hope it helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!