Android permission denied for window type 2010 in Marshmallow or higher

前端 未结 4 1483
醉酒成梦
醉酒成梦 2021-01-19 06:22

I am try to make Chathead type overly draw via Android Service on my app like bellow image.

This chat head app works on An

4条回答
  •  醉话见心
    2021-01-19 07:12

    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.

提交回复
热议问题