使用AccessibilityService模拟点击事件失败的分析
使用AccessibilityService模拟点击事件的方法:
AccessibilityNodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
但是前提是这个AccessibilityNodeInfo具有Onclick能力,也就是isClickable()为true。
问题在于控件的点击效果不一定非得通过onClick方法实现,也可以通过onTouchEvent方法实现。当通过onTouchEvent方法实现的时候,就无法通过AccessibilityNodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK)方法模拟这个控件的点击事件了。
一、点击效果通过onClick方法实现
id_btn_openFloatWindow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.i("onClick", "onClick");
}
});
二、点击效果通过onTouchEvent方法实现
id_btn_openFloatWindow.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
onTouchEvent(event,v);
return true;//改为true,则不会触发OnClick方法
}
});
private boolean onTouchEvent(MotionEvent event, View view) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.i("onTouchEvent", "ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
Log.i("onTouchEvent", "ACTION_UP");
break;
}
return true;
}
如果采用OnTouchEvent方法实现点击事件并且没有做到和辅助功能兼容,那么也就无法通过onTouchEvent模拟该控件的点击事件了。
===================================================================
onTouchEvent方法实现与辅助性服务保持兼容性的解决方案:
- 生成一个接近解释AccessibilityEvent的点击动作。
- 对不能使用触摸屏的用户提供自定义点击动作的辅助性服务。
class CustomTouchView extends View {
public CustomTouchView(Context context) {
super(context);
}
boolean mDownTouch = false;
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
// Listening for the down and up touch events
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mDownTouch = true;
return true;
case MotionEvent.ACTION_UP:
if (mDownTouch) {
mDownTouch = false;
performClick(); // Call this method to handle the response, and
// thereby enable accessibility services to
// perform this action for a user who cannot
// click the touchscreen.
return true;
}
}
return false; // Return false for other touch events
}
@Override
public boolean performClick() {
// Calls the super implementation, which generates an AccessibilityEvent
// and calls the onClick() listener on the view, if any
super.performClick();
// Handle the action for the custom click here
return true;
}
}
上面所示的模式通过调用performClick()方法确保了自定义点击事件是与辅助性服务兼容的,performClick()方法既生成一个辅助性事件,又提供一个访问辅助性服务的入口,来代表用户执行了自定义的点击事件。
参考资料:
http://www.xuebuyuan.com/2061597.html
使用AccessibilityService模拟点击事件失败的分析的更多相关文章
- js模拟点击事件实现代码
js模拟点击事件实现代码 类型:转载 时间:2012-11-06 在实际的应用开发中,我们会常常用到JS的模事件,比如说点击事件,举个简单的例子,点击表单外的"提交"按钮来提交表单 ...
- jquery click()方法模拟点击事件对a标签不生效的解决办法
阅读数:8971 <a href="www.baidu.com"></a> 1 问题分析 点击A标签本身,并不会触发跳转到指定链接的事件,就是说,我们平时都 ...
- jquery click()方法模拟点击事件对a标签不生效
if(e.keyCode == 13) { $items.eq(index).click(); return; } 搜索框下拉列表模拟点击时间,使用上述代码不能触发链接跳转 1,页面使用了bootst ...
- jquery模拟点击事件
在某些情况下,我们需要自动执行一些点击事件.比如:一些 tab 一般是通过点击事件来加载不同的数据内容. 而如果要页面加载完直接显示第三个 tab,怎么办呢?此时就需要用到 jQuery 的模拟点击事 ...
- adb shell模拟点击事件(input tap)
前言:appium定位也不是万能的,有些元素还是定位不到,这个时候只能换一个方式定位了,可以使用这个adb shell模拟点击. 1.input可以实现的功能 输入文本信息:input text gu ...
- 使用adb shell 模拟点击事件
针对问题[appium无法点击到某一内容,需要使用adb去执行点击事件] 需要命令: adb shell adb devices input [input可以实现的功能]: 输入文本信息:input ...
- 【CSS-进阶之元素:focus伪类模拟点击事件】
先放上我们最终实现的效果 注:这里建议插入codepen(临时使用图片代替) 我们想要实现当点击某个元素时,显示一个tip浮动框. html: <div class="wrapper& ...
- appium+python自动化37-adb shell模拟点击事件(input tap)
前言 appium有时候定位一个元素很难定位到,或者说明明定位到这个元素了,却无法点击,这个时候该怎么办呢? 求助大神是没用的,点击不了就是点击不了,appium不是万能的,这个时候应该转换思路,换其 ...
- appium+python自动化-adb shell模拟点击事件(input tap)
前言 appium有时候定位一个元素很难定位到,或者说明明定位到这个元素了,却无法点击,这个时候该怎么办呢? 求助大神是没用的,点击不了就是点击不了,appium不是万能的,这个时候应该转换思路,换其 ...
随机推荐
- bzoj 2190 仪仗队(欧拉函数)
2190: [SDOI2008]仪仗队 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2245 Solved: 1413[Submit][Statu ...
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor (链表)
题目链接:http://codeforces.com/contest/670/problem/E 给你n长度的括号字符,m个操作,光标初始位置是p,'D'操作表示删除当前光标所在的字符对应的括号字符以 ...
- Grand Central Dispatch(GCD)编程基础
GCD:线程 http://blog.csdn.net/nono_love_lilith/article/details/7829557
- Cloud Computing Deployment Models
Cloud computing can broadly be broken down into three main categories based on the deployment model. ...
- Unity3D - 关于Dynamic和Static
含有Collider和RigidBody的GameObject, Unity视之为Dynamic 含有Collider的GameObject, Unity视之为Static 如果Static的物体发生 ...
- 标准C++ 字符串处理增强函数
转自:http://dewei.iteye.com/blog/1566734 //标准C++ string 去除首尾空白字符 2012-8-12 By Dewei static inline void ...
- skyline TerraExplorer fly设置相对路径的方法
软件环境:TerraExplorer Pro(以下简称TEP)6.5 在TEP中,对于本地(非网络)文件路径,默认都是绝对路径,在移动数据时非常麻烦,以下是本人总结出一些设置相对路径的规则 假设fly ...
- Codeforces Round #329 (Div. 2) B. Anton and Lines 逆序对
B. Anton and Lines Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/593/pr ...
- Html&CSS 今日心得
今天和秋秋一起review了一下我自己写的登录页面.她给我提了几个point,对我很有启发. css样式的代码和html代码分离. 我自己做的时候是在google console里面调好了样式以后就直 ...
- stl string
10.2.1 STL的string 1String概念 ² string是STL的字符串类型,通常用来表示字符串.而在使用string之前,字符串通常是用char*表示的.string与char*都 ...