假设您已经设置AndroidManifest
了识别AccessibilityService
,我将在此处添加其他必需的代码,以防有人想要直接回答。
无障碍服务配置文件:
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android= "http://schemas.android.com/apk/res/android"
android:description="@string/accessibility_service_description"
android:accessibilityEventTypes="typeWindowStateChanged|typeViewTextChanged"
android:accessibilityFlags="flagDefault"
android:accessibilityFeedbackType="feedbackSpoken"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity"/>
请注意android:accessibilityEventTypes
根据需要。如果您将包限制在 xml 中,您将无法得到TYPE_WINDOW_STATE_CHANGED
问题中正确指出的内容。
因此,您还需要对AccessibilityService
.
public class VoiceAccessibilityService extends android.accessibilityservice.AccessibilityService {
private static String TAG = VoiceAccessibilityService.class.getName();
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED && event.getPackageName().equals("Your package names")) {
Logger.d(TAG + " SHOW " + event.toString());
}
}
@Override
public void onInterrupt() {
Logger.d(TAG + " interrupt");
}
}
希望能帮助到你!