如何在Android中单击浮动按钮时通过AccessibilityService执行滚动事件?

时间:2017-04-17 23:20:43

标签: android android-accessibility

我有Service发起Button并通过调用WindowManager.addView()添加它。此按钮始终显示在所有内容之上。

另一个Service扩展了AccessibilityService。我确实收到了onAccessibilityEvent中的活动,但未在另一个Button中创建的Service上发生点击事件。我确实收到了其他观点的点击事件。另一个问题是当用户点击在另一个onAccessibilityEvent中创建的Button时如何触发Service

我不知道为什么Button中创建的Service不会触发onAccessibilityEvent

这是我的FloatingButton

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() {

    }
}

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

感谢!!!

1 个答案:

答案 0 :(得分: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不允许后台点击。