问题描述
我的要求:从弹出窗口,对话框等特定应用程序中读取文本.
My requirement: Reading the text from pop up, dialog etc for particular app.
我已经实施了无障碍服务,并且根据我的要求,我正在接收适当的事件和数据.但是,在测试时,我意识到在某些设备上,他们没有使用AlertDialog或Dialog,而是使用了活动(以对话框为主题).因此,在可访问性事件中,我仅收到活动标题,是否可以找到该特定弹出活动显示的文本?
I have implemented an accessibility service and I am receiving proper events and data as per my requirement. However while testing I realized that on some devices instead of using a AlertDialog or Dialog they have used an activity(themed as a dialog).Hence in my accessibility event I receive only the activity title, is there a way I could find the text displayed by this particular pop up activity?
我已经进行了艰苦的搜索,但是在该主题上没有太多帮助,文档在此方面也没有任何帮助.可访问性服务的代码很少,但是如果您仍然需要,我会在以后发布.
I have searched pretty hard but could not get much help on the topic nor did the documentation be of any good in this concern. There is not much in the code of accessibility service but if you still need I will post it later.
谢谢
推荐答案
即使在手机返回的情况下,也使用accessiblityNodeInfo获取信息,它将获取usdd响应,并且在可以选择输入多个选项时也将关闭对话框
Use accessiblityNodeInfo to get information even in the case of when phone is returning it will fetch the ussd response also it will dismiss dialog when there is option to enter multiple choices.
首先,如果将[pohne]事件类名称作为ussdalertactivty返回,那么我只使用"alert"来标识usd响应的警报对话框.
First of all in case of [pohne] event class name is returned as ussdalertactivty so i used only "alert" for identification of alert dialog of ussd response.
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);
}
}
现在,我已经创建了一个名为fetchResponse的函数,该函数将以字符串形式返回pcn的响应,并且还将关闭该对话框,因此需要执行performGlobalAction(GLOBAL_ACTION_BACK).
Now i have made a function called fetchResponse which will return the response from pcn as string and will also dismiss the dialog so need to do 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;
}
希望这将解决该问题,而与设备无关.
Hope this will solve the issue irrespective of the devices.
这篇关于Android无法使用辅助功能在少数设备上读取窗口内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!