Android无障碍模式模拟收集屏幕点击

无障碍模拟点击

  1. 创建无障碍服务MyAccessibilityService
    public class MyAccessibilityService extends AccessibilityService {
    
        private EventObserver<SimulateClickBean> observer = new EventObserver<SimulateClickBean>() {
            @Override
            public void onObserver(SimulateClickBean clickBean) {
                simulateClick(clickBean.getX(), clickBean.getY());
            }
        };
    
        @Override
        protected void onServiceConnected() {
            super.onServiceConnected();
            LtEventBus.with(EventKey.KEY_EVENT_SIMULATE_CLICK, SimulateClickBean.class)
                    .observer(observer);
        }
    
        @Override
        public void onAccessibilityEvent(AccessibilityEvent event) {
            LogUtils.d(TAG, " onAccessibilityEvent " + stringBuilder.toString());
        }
    
        @Override
        public void onInterrupt() {
            // handle interrupt
        }
    
    }
    

  2. AndroidManifest.xml中声明服务
    <service
        android:name=".service.MyAccessibilityService"
        android:exported="true"
        android:label="模拟点击"
        android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
        <intent-filter>
            <action android:name="android.accessibilityservice.AccessibilityService" />
        </intent-filter>
        <meta-data
            android:name="android.accessibilityservice"
            android:resource="@xml/accessibility_config" />
    </service>

  3. accessibility_config.xml文件
    <?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:canPerformGestures="true"
        android:canRetrieveWindowContent="true"
        android:notificationTimeout="100" />

  4. 无障碍服务模拟屏幕点击
  5.     public void simulateClick(int x, int y) {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                LogUtils.d(TAG, "simulateClick ", "X: " + x, "Y: " + y);
                Path path = new Path();
                path.moveTo(x, y);
                path.lineTo(x, y);
                GestureDescription.Builder builder = new GestureDescription.Builder();
                builder.addStroke(new GestureDescription.StrokeDescription(path, 0, 100));
                dispatchGesture(builder.build(), null, null);
            }
        }
  6.  打开无障碍服务设置
     Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
     startActivity(intent);

系统悬浮窗 

  1. 申请悬浮窗权限
    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
    Uri.parse("package:" + activity.getPackageName()));
    activity.startActivityForResult(intent, REQUEST_CODE);
    private boolean canDrawOverlays(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(context)) {
            return false;
        }
        return true;
    }

  2. 添加悬浮窗到windows
    private void addViewToWindow(Context context, View contentView, int x, int y) {
            WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
                    PixelFormat.TRANSLUCENT);
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
            } else {
                params.type = WindowManager.LayoutParams.TYPE_PHONE;
            }
            params.x = x;
            params.y = y;
            params.alpha = 0.5F;
            params.dimAmount = 0.3f;
            params.gravity = Gravity.START | Gravity.TOP;
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            windowManager.addView(contentView, params);
            addStatusMap.put(String.valueOf(contentView.getId()), true);
        }
        private void remove(Context context, View view) {
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            if (view != null && addStatusMap.containsKey(String.valueOf(view.getId()))) {
                windowManager.removeView(view);
                addStatusMap.remove(String.valueOf(view.getId()));
            }
        }

  3. 更新悬浮窗位置
       private void updateViewLayoutParam(Context context, View view, WindowManager.LayoutParams layoutParams) {
            if (System.currentTimeMillis() - updateInterval < 10) {
                return;
            }
            updateInterval = System.currentTimeMillis();
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            windowManager.updateViewLayout(view, layoutParams);
        }
    mOverlayViewBinding.getRootView().setOnTouchListener(new View.OnTouchListener() {
                int downX, downY;
                int moveX, moveY;
    
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    switch (motionEvent.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            downX = (int) motionEvent.getX();
                            downY = (int) motionEvent.getY();
                            LogUtils.d(TAG, "ACTION_DOWN");
                            break;
                        case MotionEvent.ACTION_MOVE:
                            moveX = (int) motionEvent.getX();
                            moveY = (int) motionEvent.getY();
                            WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams)
                                    mOverlayViewBinding.getRootView().getLayoutParams();
                            layoutParams.x = layoutParams.x + (int) (0.8F * (moveX - downX));
                            layoutParams.y = layoutParams.y + (int) (0.8F * (moveY - downY));
                            layoutParams.gravity = Gravity.START | Gravity.TOP;
                            updateViewLayoutParam(view.getContext(), mOverlayViewBinding.getRootView(),
                                    layoutParams);
                            downX = moveX;
                            downY = moveY;
                            break;
                        case MotionEvent.ACTION_UP:
                            LogUtils.d(TAG, "ACTION_UP");
                            break;
                    }
                    return true;
                }
            });

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值