简体   繁体   English

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

[英]How to use accessibility service in Flutter - Platform Specific

I'm trying to integrate accessibility service in the flutter based application and I dug into it and found out that I will have to use method channels in order to achieve this task because it is possible only by using android native code (java/kotlin)我正在尝试在基于 flutter 的应用程序中集成辅助功能服务,我深入研究它并发现我必须使用方法通道才能完成此任务,因为只有使用 android 本机代码 (java/kotlin) 才有可能

As you know, We must write the code inside MainActivity that extends FlutterActivity in order to make it work.如您所知,我们必须在 MainActivity 中编写扩展 FlutterActivity 的代码才能使其工作。 How to extend AccessibilityService in this platform-specific code?如何在此特定于平台的代码中扩展 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
          }
        );
  }
}

This is the code that I'm trying to implement in Flutter and one question, do I need to invoke this code from the Flutter client end?这是我试图在 Flutter 中实现的代码,还有一个问题,我是否需要从 Flutter 客户端调用此代码? because as far as I know, accessibility service code is automatically called by the android system因为据我所知,无障碍服务代码是由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() {
    }

...
}

days ago I made a flutter plugin to interact with Accessibility Service in Android. it's easy to use just follow the instruction in the readme and check the example for more details flutter_accessibility_service几天前,我制作了一个 flutter 插件,用于与 Android 中的辅助功能服务进行交互。只需按照自述文件中的说明进行操作即可轻松使用,并查看示例以了解更多详细信息flutter_accessibility_service

this is an example of how to use it这是一个如何使用它的例子

 /// 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
)
  */
  
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM