gpt4 book ai didi

android - AccessibilityService 无法接收 onKeyEvent 或 onGesture

转载 作者:行者123 更新时间:2023-11-30 02:01:59 25 4
gpt4 key购买 nike

我是一名正在研究Android安全的中国学生。(我的句子可能有一些语法错误)我想使用辅助功能API来捕获用户的行为,例如点击IME的键盘并选择某些字符.. 但是当我尝试这个 API 时,我只能获取当前窗口的文本和用户执行的操作,而不是来自键盘的任何事件。也许我错过了什么。(我无法从 onKeyEvent 和 onGesture 接收任何事件。但我可以从 onAccessibilityEvent 接收事件。)

我已经创建了以下基本的无障碍服务。

package com.jack.accessibility;

import java.util.List;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.annotation.SuppressLint;
import android.util.Log;
import android.view.KeyEvent;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

@SuppressLint("NewApi")
public class MyAccessibilityService extends AccessibilityService {
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
String eventText = getTypeName(event.getEventType()) + "===="
+ event.getContentDescription();
Log.e("------------------------", "-------------------------");
Log.d("PackageName", event.getPackageName().toString());
Log.d("EventName", eventText);
traverseNode(getRootInActiveWindow());
Log.e("------------------------", "-------------------------");
}

@Override
public void onServiceConnected() {
Log.e("---------ServiceConnected--------------",
"------------ServiceConnected------------");
AccessibilityServiceInfo info = new AccessibilityServiceInfo();
info.packageNames = new String[] { "com.android.mms" };
info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
info.notificationTimeout = 100;
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
info.flags = AccessibilityServiceInfo.FLAG_REQUEST_FILTER_KEY_EVENTS;
setServiceInfo(info);
}

@Override
public boolean onKeyEvent(KeyEvent event) {
Log.i("Key", ".....");
return true;
}

@Override
public boolean onGesture(int gestureId) {
Log.v("THEIA", String.format("onGesture: [type] %s", gIdToString(gestureId)));
return false;
}

@Override
public void onInterrupt() {
Log.e("---------Interrupt--------------",
"------------Interrupt------------");
}

private void traverseNode(AccessibilityNodeInfo node) {
if (null == node)
return;

final int count = node.getChildCount();
if (count > 0) {
for (int i = 0; i < count; i++) {
AccessibilityNodeInfo childNode = node.getChild(i);
traverseNode(childNode);
}
} else {
CharSequence text = node.getText();
Log.d("test", "Node text = " + text);
}

}
private String getTypeName(int type) {
switch (type) {
case AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_START:
return "TYPE_TOUCH_EXPLORATION_GESTURE_START";
case AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_END:
return "TYPE_TOUCH_EXPLORATION_GESTURE_END";
case AccessibilityEvent.TYPE_TOUCH_INTERACTION_START:
return "TYPE_TOUCH_INTERACTION_START";
case AccessibilityEvent.TYPE_TOUCH_INTERACTION_END:
return "TYPE_TOUCH_INTERACTION_END";
case AccessibilityEvent.TYPE_GESTURE_DETECTION_START:
return "TYPE_GESTURE_DETECTION_START";
case AccessibilityEvent.TYPE_GESTURE_DETECTION_END:
return "TYPE_GESTURE_DETECTION_END";
case AccessibilityEvent.TYPE_VIEW_HOVER_ENTER:
return "TYPE_VIEW_HOVER_ENTER";
case AccessibilityEvent.TYPE_VIEW_HOVER_EXIT:
return "TYPE_VIEW_HOVER_EXIT";
case AccessibilityEvent.TYPE_VIEW_SCROLLED:
return "TYPE_VIEW_SCROLLED";
case AccessibilityEvent.TYPE_VIEW_CLICKED:
return "TYPE_VIEW_CLICKED";
case AccessibilityEvent.TYPE_VIEW_LONG_CLICKED:
return "TYPE_VIEW_LONG_CLICKED";
case AccessibilityEvent.TYPE_VIEW_FOCUSED:
return "TYPE_VIEW_FOCUSED";
case AccessibilityEvent.TYPE_VIEW_SELECTED:
return "TYPE_VIEW_SELECTED";
case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED:
return "TYPE_VIEW_ACCESSIBILITY_FOCUSED";
case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED:
return "TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED";
case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
return "TYPE_WINDOW_STATE_CHANGED";
case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
return "TYPE_NOTIFICATION_STATE_CHANGED";
case AccessibilityEvent.TYPE_ANNOUNCEMENT:
return "TYPE_ANNOUNCEMENT";
case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:
return "TYPE_WINDOW_CONTENT_CHANGED";
case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED:
return "TYPE_VIEW_TEXT_CHANGED";
case AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED:
return "TYPE_VIEW_TEXT_SELECTION_CHANGED";
case AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY:
return "TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY";
}
return "Unknown";
}

private String gIdToString(int gID) {
switch (gID) {
case 1:
return "GESTURE_SWIPE_UP";
case 2:
return "GESTURE_SWIPE_DOWN";
case 3:
return "GESTURE_SWIPE_LEFT";
case 4:
return "GESTURE_SWIPE_RIGHT";
case 5:
return "GESTURE_SWIPE_LEFT_AND_RIGHT";
case 6:
return "GESTURE_SWIPE_RIGHT_AND_LEFT";
case 7:
return "GESTURE_SWIPE_UP_AND_DOWN";
case 8:
return "GESTURE_SWIPE_DOWN_AND_UP";
case 9:
return "GESTURE_SWIPE_LEFT_AND_UP";
case 10:
return "GESTURE_SWIPE_LEFT_AND_DOWN";
case 11:
return "GESTURE_SWIPE_RIGHT_AND_UP";
case 12:
return "GESTURE_SWIPE_RIGHT_AND_DOWN";
case 13:
return "GESTURE_SWIPE_UP_AND_LEFT";
case 14:
return "GESTURE_SWIPE_UP_AND_RIGHT";
case 15:
return "GESTURE_SWIPE_DOWN_AND_LEFT";
case 16:
return "GESTURE_SWIPE_DOWN_AND_RIGHT";
}
return "UNKNOWN";
}
}

AndroidManifest.XML:

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.jack.accessibility.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:label="@string/acc_service_name" android:name=".MyAccessibilityService" 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/phone_accessibility" />
</service>

</application>

phone_accessibility.xml:

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service android:description="@string/accessibility_service_description"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFeedbackType="feedbackAllMask"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
android:canRequestTouchExplorationMode="true"
android:canRequestEnhancedWebAccessibility="true"
android:canRequestFilterKeyEvents="true"
android:accessibilityFlags="flagDefault"
xmlns:android="http://schemas.android.com/apk/res/android" />

最佳答案

添加这个:

info.flags = AccessibilityServiceInfo.FLAG_REQUEST_TOUCH_EXPLORATION_MODE;

关于android - AccessibilityService 无法接收 onKeyEvent 或 onGesture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31312588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com