悬浮窗口无法回调Accessibilityservice

枕霞旧友 2016-07-19 07:08:23
EventType配置是typeAllMask,但点击悬浮窗口根本收不到任何事件,这时候点击诸如桌面图标等都可以收到事件,但悬浮窗口却可以设置点击监听,why?


EventType配置:
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFeedbackType="feedbackGeneric"
android:accessibilityFlags=""
android:canRetrieveWindowContent="true"
android:description="@string/accessibility_service_description"
android:notificationTimeout="100" />


悬浮窗口设置:
mWindowManager = (WindowManager) getApplicationContext().getSystemService("window");

mLayout = new WindowManager.LayoutParams();

mLayout.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;

mLayout.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

mLayout.format = PixelFormat.RGBA_8888;

mLayout.gravity = Gravity.TOP | Gravity.LEFT;

mLayout.width = WindowManager.LayoutParams.MATCH_PARENT;

mLayout.height = WindowManager.LayoutParams.WRAP_CONTENT;
...全文
477 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
chamber_of_secrets 2016-07-20
  • 打赏
  • 举报
回复
我也是新手,正好最近在研究这个,我实现的效果是一个悬浮的窗口点击之后出现一个popuwindow,组件放在popuwindow里
private void createFloatView() {


        wmParams = new WindowManager.LayoutParams();

        mWindowManager = (WindowManager) getApplication().getSystemService(getApplication().WINDOW_SERVICE);

        wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;

        wmParams.format = PixelFormat.RGBA_8888;

        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

        wmParams.gravity = Gravity.LEFT | Gravity.TOP;

        wmParams.x = 0;
        wmParams.y = 0;

        wmParams.width = 150;
        wmParams.height = 150;

        LayoutInflater inflater = LayoutInflater.from(getApplication());

        mFloatLayout = (LinearLayout) inflater.inflate(R.layout.floatservice, null);

        mWindowManager.addView(mFloatLayout, wmParams);


        mFloatView  = (ImageView) mFloatLayout.findViewById(R.id.float_iv);


        mFloatLayout.measure(View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
                .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));


        mFloatView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                //getRawX是触摸位置相对于屏幕的坐标,getX是相对于按钮的坐标
                wmParams.x = (int) event.getRawX() - mFloatView.getMeasuredWidth() / 2;
                Log.i(TAG, "RawX" + event.getRawX());
                Log.i(TAG, "X" + event.getX());
                //减25为状态栏的高度
                wmParams.y = (int) event.getRawY() - mFloatView.getMeasuredHeight() / 2 - 25;
                Log.i(TAG, "RawY" + event.getRawY());
                Log.i(TAG, "Y" + event.getY());
                //刷新
                mWindowManager.updateViewLayout(mFloatLayout, wmParams);
                return false;  //此处必须返回false,否则OnClickListener获取不到监听
            }
        });

        mFloatView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (popupWindow != null && popupWindow.isShowing())
                    popupWindow.dismiss();
                else
                    initPopWindow();
            }
        });
    }
protected void initPopWindow() {

        //        List<Msg> msgList=new ArrayList<Msg>();
//        MessageAdapter adapter;
//
//        initData(msgList);
//        adapter = new MessageAdapter(getApplicationContext(),R.layout.msg_item, msgList);


        LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
        View view = inflater.inflate(R.layout.floatwindowservice, null);
    //    ListView listview = (ListView) view.findViewById(R.id.listview);

        popupWindow = new PopupWindow(view,500,400);
        popupWindow.setContentView(view);

        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        Button close = (Button) view.findViewById(R.id.close);
        Button send = (Button) view.findViewById(R.id.send);


        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.setFocusable(false);
                popupWindow.dismiss();
            }
        });

        popupWindow.showAsDropDown(mFloatView,20,20);
枕霞旧友 2016-07-20
  • 打赏
  • 举报
回复
引用 1 楼 chamber_of_secrets 的回复:
mLayout.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE这句让你的悬浮窗口失去焦点,最近我也在研究这个,貌似不能让悬浮窗和外部同时获得焦点,我暂时在悬浮窗里放了一个按钮可以改变它的flag属性,初始时让它得到焦点,点击按钮让它失去焦点,不知道有没有高手可以给出更好的解决办法


谢谢一点多还在撸码的兄弟,我意识到了FLAG_NOT_FOCUSABLE失焦问题,于是我改用FLAG_NOT_TOUCH_MODAL(即使在该window在可获得焦点情况下,仍然把该window之外的任何event发送到该window之后的其他window),但这样back键会失效,因为焦点是在悬浮窗体上,似乎只能改变flag属性,boy能分享下改flag方案吗
chamber_of_secrets 2016-07-20
  • 打赏
  • 举报
回复
mLayout.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE这句让你的悬浮窗口失去焦点,最近我也在研究这个,貌似不能让悬浮窗和外部同时获得焦点,我暂时在悬浮窗里放了一个按钮可以改变它的flag属性,初始时让它得到焦点,点击按钮让它失去焦点,不知道有没有高手可以给出更好的解决办法

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧