我正在为Whatsapp开发一个辅助功能服务。每当用户按下输入框时,我想知道当前标题栏通常有收件人whatsapp用户或whatsapp组的名称:
我有兴趣获取该字符串Test User
这是我的xml:
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeViewClicked|typeViewFocused"
android:packageNames="com.whatsapp"
android:accessibilityFeedbackType="feedbackSpoken"
android:notificationTimeout="100"
android:settingsActivity="com.example.android.apis.accessibility.TestBackActivity"
android:canRetrieveWindowContent="true" />
这是我的代码:
public void onAccessibilityEvent(AccessibilityEvent event) {
final int eventType = event.getEventType();
String eventText = null;
switch(eventType) {
case AccessibilityEvent.TYPE_VIEW_CLICKED:
eventText = "Clicked: ";
break;
case AccessibilityEvent.TYPE_VIEW_FOCUSED:
eventText = "Focused: ";
break;
}
eventText = eventText + event.getContentDescription();
Log.d(TAG, "onAccessibilityEvent: " + eventText);
if (eventText.toLowerCase().contains("type a message")) {
// access the title bar name/string here
}
}
我尝试获取当前窗口或父窗口并检查其标题,两者似乎都为空:
AccessibilityNodeInfo currentNode = event.getSource();
AccessibilityNodeInfo parentNode = nodeInfo.getParent();
AccessibilityWindowInfo currentWindow = currentNode.getWindow();
AccessibilityWindowInfo parentWindow = parentNode.getWindow();
// currentWindow.getTitle();
// parentWindow.getTitle();
答案 0 :(得分:0)
我编写了一个库来帮助使用AccessibilityNodeInfos和其他Android Accessibility API进行处理。它几乎完成了,需要记录。但是,使用这个库你可以这样做:
在主服务中(为了允许访问root辅助功能节点),请执行以下操作。
A11yNodeInfo nodeInfo = A11yNodeInfo.wrap(getRootInActiveWindow());
A11yNodeInfo result = nodeInfo.visit(new A11yNodeInfo.OnVisitListener() {
@Override
public boolean onVisit(A11yNodeInfo nodeInfo) {
A11yNodeInfoMatcher matcher = new A11yNodeInfoMatcher();
//Add properties to the matcher as needed. I'm just guessing here!
return matcher.matches(nodeInfo);
}
});
如果要浏览该元素的辅助功能属性,还可以打印出整个辅助功能树:
Log.d("Tag", A11yNodeInfo.wrap(getRootInActiveWindow).toViewHeirarchy());
https://github.com/chriscm2006/Android-Accessibility-Utilities
答案 1 :(得分:0)
这对我有用:
error: implicit instantiation of undefined template 'TD<int>'
我的AndroidManifest.xml
AccessibilityNodeInfo nodeInfo = event.getSource();
try
{
List<AccessibilityNodeInfo> findAccessibilityNodeInfosByViewId = nodeInfo.findAccessibilityNodeInfosByViewId("com.whatsapp:id/conversation_contact_name");
if (findAccessibilityNodeInfosByViewId.size() > 0)
{
AccessibilityNodeInfo parent = (AccessibilityNodeInfo) findAccessibilityNodeInfosByViewId.get(0);
String contactName = parent.getText().toString();
if (contactName != null && !contactName.isEmpty()) // do your stuff here, contactName contains the chat contact name!
}
}
catch(Exception contactName) {}
我的onServiceConnected方法:
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityFeedbackType="feedbackAllMask"
android:accessibilityFlags="flagIncludeNotImportantViews|flagReportViewIds"
android:canRetrieveWindowContent="true"
android:packageNames="com.whatsapp"
/>