Ish*_*aan 5 android accessibility-api accessibilityservice
我的要求:从特定应用的弹出窗口,对话框等中读取文本.
我已经实现了一个辅助功能服务,我按照我的要求接收了适当的事件和数据.然而,在测试时,我意识到在某些设备上而不是使用AlertDialog或Dialog,他们使用了一个活动(主题为对话框).因此,在我的辅助功能事件中,我只收到活动标题,是否有一种方法可以找到此特定弹出活动显示的文本?
我搜索得非常努力,但在这个主题上得不到多少帮助,文档也没有任何好处.可访问性服务的代码并不多,但如果您仍然需要,我会稍后发布.
谢谢
即使在电话返回的情况下,也可以使用accessiblityNodeInfo来获取信息,它将获取ussd响应,当有选项输入多个选项时,它也会解除对话框.
首先,如果[pohne]事件类名称作为ussdalertactivty返回,所以我只使用"alert"来识别ussd响应的警报对话框.
public void onAccessibilityEvent(AccessibilityEvent event) {
if (event.getPackageName().toString().equals("com.android.phone")
&& event.getClassName().toString().toLowerCase()
.contains("alert")) {
AccessibilityNodeInfo source = event.getSource();
if (source != null) {
String pcnResponse = fetchResponse(source);
}
}
Run Code Online (Sandbox Code Playgroud)
现在我已经创建了一个名为fetchResponse的函数,它将把pcn的响应作为字符串返回,并且还会关闭对话框,因此需要执行performGlobalAction(GLOBAL_ACTION_BACK).
private String fetchResponse(AccessibilityNodeInfo accessibilityNodeInfo) {
String fetchedResponse = "";
if (accessibilityNodeInfo != null) {
for (int i = 0; i < accessibilityNodeInfo.getChildCount(); i++) {
AccessibilityNodeInfo child = accessibilityNodeInfo.getChild(i);
if (child != null) {
CharSequence text = child.getText();
if (text != null
&& child.getClassName().equals(
Button.class.getName())) {
// dismiss dialog by performing action click in normal
// cases
if((text.toString().toLowerCase().equals("ok") || text
.toString().toLowerCase()
.equals("cancel"))) {
child.performAction(AccessibilityNodeInfo.ACTION_CLICK);
return fetchedResponse;
}
} else if (text != null
&& child.getClassName().equals(
TextView.class.getName())) {
// response of normal cases
if (text.toString().length() > 10) {
fetchedResponse = text.toString();
}
} else if (child.getClassName().equals(
ScrollView.class.getName())) {
// when response comes as phone then response can be
// retrived from subchild
for (int j = 0; j < child.getChildCount(); j++) {
AccessibilityNodeInfo subChild = child.getChild(j);
CharSequence subText = subChild.getText();
if (subText != null
&& subChild.getClassName().equals(
TextView.class.getName())) {
// response of different cases
if (subText.toString().length() > 10) {
fetchedResponse = subText.toString();
}
}
else if (subText != null
&& subChild.getClassName().equals(
Button.class.getName())) {
// dismiss dialog by performing action click in
// different
// cases
if ((subText.toString().toLowerCase()
.equals("ok") || subText
.toString().toLowerCase()
.equals("cancel"))) {
subChild.performAction(AccessibilityNodeInfo.ACTION_CLICK);
return fetchedResponse;
}
}
}
}
}
}
}
return fetchedResponse;
}
Run Code Online (Sandbox Code Playgroud)
希望这将解决问题,无论设备如何.
| 归档时间: |
|
| 查看次数: |
4542 次 |
| 最近记录: |