系统相关
首页 > 系统相关> > 棉花糖或更高版本的Windows类型2010的Android权限被拒绝

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

作者:互联网

我试图像我的波纹管图像一样通过我的应用程序上的Android服务过度绘制Chathead类型.

enter image description here

这个聊天头应用程序可在Android版本5或更低版本(kitkat,lollipop等)上运行.但是我正在棉花糖和更高版本中尝试使用,然后出现此错误.

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

enter image description here

聊天头代码

注意:我正在从Android Service调用此函数

权限列表:

<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;
            }
        }
    }

如何解决Mashmallow或更高版本的Android上的此错误?

解决方法:

在显示聊天标题之前,请调用此方法以请求许可:

 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);
        }
    }
}

在许可结果上,如果用户允许许可,则只有您可以显示聊天头,如下所示:

 @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();
        }
    }

让我知道它是否对您有帮助…祝您好运.

标签:android,android-permissions,android-layout
来源: https://codeday.me/bug/20191011/1889847.html