flutter_accessibility_service

一个用于与Android中的可访问性服务进行交互的插件。

可访问性服务旨在帮助残疾用户使用Android设备和应用程序,或者说获取Android操作系统事件,例如键盘按键事件或通知接收事件等。

有关更多信息,请参阅 可访问性服务

安装和使用

将包添加到您的 pubspec

dependencies:
  flutter_accessibility_service: any # or the latest version on Pub

在AndroidManifest中添加此项以将您的可访问性服务与您的应用程序绑定

    ...
    <service android:name="slayer.accessibility.service.flutter_accessibility_service.AccessibilityListener" 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/accessibilityservice" />
        </service>
    ...
</application>

res/xml 中创建名为 accessibilityservice.xml 的可访问性配置文件,并在其中添加以下代码

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes = "typeWindowsChanged|typeWindowStateChanged|typeWindowContentChanged"
    android:accessibilityFeedbackType="feedbackVisual"
    android:notificationTimeout="300"
    android:accessibilityFlags="flagDefault|flagIncludeNotImportantViews|flagRequestTouchExplorationMode|flagRequestEnhancedWebAccessibility|flagReportViewIds|flagRetrieveInteractiveWindows"
    android:canRetrieveWindowContent="true"
>
</accessibility-service>

用法

 /// check if accessibility permession is enebaled
 final bool status = await FlutterAccessibilityService.isAccessibilityPermissionEnabled();
 
 /// request accessibility permission
 /// it will open the accessibility settings page
 await FlutterAccessibilityService.requestAccessibilityPermission();
 
 /// stream the incoming Accessibility events
  FlutterAccessibilityService.accessStream.listen((event) {
    log("Current Event: $event");
  
  /*
  Current Event: AccessibilityEvent: (
     Action Type: 0 
     Event Time: 2022-04-11 14:19:56.556834 
     Package Name: com.facebook.katana 
     Event Type: EventType.typeWindowContentChanged 
     Captured Text: events you may like 
     content Change Types: ContentChangeTypes.contentChangeTypeSubtree 
     Movement Granularity: 0
     Is Active: true
     is focused: true
     in Pip: false
     window Type: WindowType.typeApplication
)
  */
  
  });

AccessibilityEvent 提供

  /// the performed action that triggered this event
  int? actionType;
  
  /// the time in which this event was sent.
  DateTime? eventTime;
  
  /// the package name of the source
  String? packageName;
  
  /// the event type.
  EventType? eventType;
  
  /// Gets the text of this node.
  String? capturedText;
  
  /// the bit mask of change types signaled by a `TYPE_WINDOW_CONTENT_CHANGED` event or `TYPE_WINDOW_STATE_CHANGED`. A single event may represent multiple change types
  ContentChangeTypes? contentChangeTypes;
  
  /// the movement granularity that was traversed
  int? movementGranularity;
  
  /// the type of the window
  WindowType? windowType;
  
  /// check if this window is active. An active window is the one the user is currently touching or the window has input focus and the user is not touching any window.
  bool? isActive;
  
  /// check if this window has input focus.
  bool? isFocused;
  
  /// Check if the window is in picture-in-picture mode.
  bool? isPip;

每个事件。

GitHub

查看 Github