2

我有一个Service启动 aButton并通过调用添加它WindowManager.addView()。此按钮始终显示在所有内容之上。

还有一个Service延伸AccessibilityService。我确实收到了事件onAccessibilityEvent,但没有点击事件发生Button在另一个中创建的事件上Service。我确实收到了其他视图的点击事件。onAccessibilityEvent另一个问题是当用户点击Button在另一个中创建的时如何触发Service

我不知道为什么Buttoncreated inService不会触发onAccessibilityEvent

这是我的浮动按钮

public class FloatingButton extends Service {
    private WindowManager wm;
    private WindowManager.LayoutParams mButtonParams;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        mButtonParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
        mButtonParams.gravity = Gravity.TOP | Gravity.START;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        startFloatingButton();
        return START_STICKY;
    }

    private void startFloatingButton() {
        Button btn = new Button(this);
        btn.setText("scroll");
        wm.addView(btn, mButtonParams);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // should fire onAccessibilityEvent() 
            }
        });
    }
}

和我的AccessibilityService

public class MyAccessibilityService extends AccessibilityService {
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        Log.v(TAG, "event type: " + event.getEventType());
    }

    @Override
    public void onInterrupt() {

    }
}

我需要Button在 FloatingButton 中创建的Service或者触发事件onAccessibilityEvent或者执行的方式AccessibilityNodeInfo.ACTION_SCROLL_FORWARD。但没有AccessibilityEvent,我不知道如何执行滚动操作。另外,有没有办法在执行操作之前检查当前视图是否可滚动?

谢谢!!!

4

1 回答 1

0

改变

mButtonParams = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

mButtonParams = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

并将其放入onServiceConnected()您的无障碍服务的方法中。只有在您的无障碍服务中创建窗口管理器,您才能获得无障碍事件。对于上下滚动,

nodeInfo.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
nodeInfo.performAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);

TYPE_PHONE 不允许背景点击。

于 2017-10-23T09:02:19.250 回答