如何从活动中发送手势

问题描述

当我点击按钮时,我试图发送一个手势,但没有任何反应,为什么?

我将手势位置放在按钮的顶部,以检查它是否有效。

我启用了无障碍权限。

主类

public class MainActivity extends AppCompatActivity {

    MyService myService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void click(View view) {
        if (myService == null)
            myService = new MyService();
        Log.w("ABC",String.valueOf(new Random().nextInt(1000)));
    }

}

MyService 类

public class MyService extends AccessibilityService {

    public MyService() {
        clickOnPosition(540,960);
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {

    }

    @Override
    public void onInterrupt() {

    }

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
    }

    private void clickOnPosition(float x,float y) {
        Path path = new Path();
        path.moveto(x,y);
        GestureDescription.strokeDescription strokeDescription = new GestureDescription.strokeDescription(path,4000,1000);
        GestureDescription.Builder builder = new GestureDescription.Builder();
        builder.addstroke(strokeDescription);
        dispatchGesture(builder.build(),new GestureResultCallback() {
            @Override
            public void onCompleted(GestureDescription gestureDescription) {
                super.onCompleted(gestureDescription);
                Log.w("ABC","Completed");
            }

            @Override
            public void onCancelled(GestureDescription gestureDescription) {
                super.onCancelled(gestureDescription);
                Log.w("ABC","Canceled");
            }
        },null);
    }

}

注意:如果我移动了 clickOnPosition(540,960),它会起作用;到 onServiceConnected()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...