如何在Flutter中使用无障碍服务-特定于平台

ivqmmu1c  于 2023-06-07  发布在  Flutter
关注(0)|答案(2)|浏览(391)

我试图在基于flutter的应用程序中集成可访问性服务,我深入研究了一下,发现我必须使用方法通道才能实现此任务,因为只有使用Android原生代码(java/Kotlin)才有可能实现此任务
如你所知,我们必须在扩展FlutterActivity的MainActivity中编写代码,以便使其工作。如何在这个特定于平台的代码中扩展AccessibilityService?

public class MainActivity extends FlutterActivity {
  private static final String CHANNEL = "samples.flutter.dev/battery";

  @Override
  public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
  super.configureFlutterEngine(flutterEngine);
    new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
        .setMethodCallHandler(
          (call, result) -> {
            // Note: this method is invoked on the main thread.
            // TODO
          }
        );
  }
}

这是我试图在Flutter中实现的代码,还有一个问题,我需要从Flutter客户端调用这些代码吗?因为据我所知,辅助功能服务代码是由Android系统自动调用的

public class MyAccessibilityService extends AccessibilityService {
...
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
    if (event.getPackageName().toString().equals("com.whatsapp")){
        StringBuilder message = new StringBuilder();
        if (!event.getText().isEmpty()) {
            for (CharSequence subText : event.getText()) {
                message.append(subText);
            }
            if (message.toString().contains("Message from")){
                name=message.toString().substring(13);
            }
        }
    }
}
    }

    @Override
    public void onInterrupt() {
    }

...
}
sdnqo3pr

sdnqo3pr1#

几天前,我做了一个flutter插件,用于与Android中的Accessibility Service交互。它很容易使用,只需按照自述文件中的说明并检查示例以获取更多细节flutter_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
)
  */
  
  });

相关问题