微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

访问具有浮动视图的其他应用上的剪贴板android10 及更高版本

如何解决访问具有浮动视图的其他应用上的剪贴板android10 及更高版本

我正在制作一个应用程序来读取剪贴板中的内容,当用户在其他应用程序的界面上时,需要从剪贴板读取文本。但是从 Android10 开始,剪贴板只能在应用获得焦点时访问。所以我创建了一个浮动视图来在用户离开我的应用程序时获得焦点。 但是有问题—— 1.当 FLAG_NOT_FOCUSABLE 设置为 layoutParams 时,我无法访问剪贴板。 2.如果没有设置FLAG_NOT_FOCUSABLE,我的浮动视图会屏蔽用户的所有按键事件,导致用户无法使用“返回”按钮。

这是我的代码

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        }

        layoutParams.format = PixelFormat.TRANSLUCENT;

        layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
        ;

        layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.x = AppContext.getSharedPreference().getInt(AppConfig.PREFERENCE_FLOATING_BUTTON_X_POSITION,AppContext.getResource().getdisplayMetrics().widthPixels);
        layoutParams.y = AppContext.getSharedPreference().getInt(AppConfig.PREFERENCE_FLOATING_BUTTON_Y_POSITION,(int) Math.floor(AppContext.getResource().getdisplayMetrics().heightPixels*0.6));
        if (Settings.canDrawOverlays(AppContext.getContext())) {
            LayoutInflater layoutInflater = LayoutInflater.from(this /* your service */);
            displayView = layoutInflater.inflate(R.layout.floating_button_layout,null);
            displayView.setFocusable(true);
            displayView.setFocusableInTouchMode(true);
            displayView.setFocusedByDefault(false);
            ImageView floatimage = displayView.findViewById(R.id.floatimage);
            ConstraintLayout constraintLayout = displayView.findViewById(R.id.floatimageContainer);


            displayView.setonClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                        ClipboardManager clipboard = (ClipboardManager) AppContext.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                        if(clipboard.hasPrimaryClip()){

                                String clipText = clipboard.getPrimaryClip().getItemAt(0).getText().toString();
                                // do some thing with clipboard content


                        }
                        displayView.clearFocus();




                }
            });

            layoutParams.windowAnimations = R.style.floating_button_animation;
            windowManager.addView(displayView,layoutParams);


有什么解决办法吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。