我想讀取用戶在其瀏覽器中輸入的網址。這是我的無障礙服務代碼。Android:使用輔助功能服務閱讀Google Chrome URL
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityFlags="flagDefault"
android:accessibilityEventTypes="typeWindowStateChanged"
android:accessibilityFeedbackType="feedbackGeneric"
android:notificationTimeout="0"
android:canRetrieveWindowContent="true"
android:packageNames="com.android.chrome"
android:description="@string/accessibility_description"
/>
在AndroidManifest
<service android:name=".MyAccessibilityService"
android:label="@string/accessibility_title"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService"/>
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/accessibility_service_config" />
</service>
在MyAccessibilityService
public void onAccessibilityEvent(AccessibilityEvent event) {
debug("On accessibility event");
getChromeUrl(getRootInActiveWindow());
}
public void getChromeUrl(AccessibilityNodeInfo nodeInfo) {
//Use the node info tree to identify the proper content.
//For now we'll just log it to logcat.
Log.d(TAG, toStringHierarchy(nodeInfo, 0));
}
private String toStringHierarchy(AccessibilityNodeInfo info, int depth) {
if (info == null) return "";
String result = "|";
for (int i = 0; i < depth; i++) {
if (result.contains("http")) {
Log.d(TAG, "Found URL!!!!!!!!!!!!!!" + result);
}
result += " ";
}
result += info.toString();
for (int i = 0; i < info.getChildCount(); i++) {
result += "\n" + toStringHierarchy(info.getChild(i), depth + 1);
}
return result;
}
private static void debug(Object object) {
Log.d(TAG, object.toString());
}
問題是我收到的網址從內容的看法在我rootview,不上頭地址欄中。任何幫助表示讚賞。
回收您訪問的孩子是一個好主意:'AccessibilityNodeInfo child = info.getChild(i); DFS(孩子); child.recycle();' – Ishamael
@Ishamael:謝謝你。更新 – cegprakash
@cegprakash有沒有辦法改變網址並將Chrome重定向到不同的頁面?你可以幫助這個鏈接:https://stackoverflow.com/questions/42731897/redirect-chrome-programmatically – kash