如何在平台特定的平台中使用可访问性服务

发布于 2025-01-21 15:54:10 字数 1554 浏览 0 评论 0原文

我正在尝试将可访问性服务集成到基于颤音的应用程序中,并挖掘出来,发现我必须使用方法频道来实现此任务,因为仅通过使用Android Antive Code(Java/Kotlin)才有可能

如您所知,我们必须在MainActivity内部编写扩展剧动性的代码以使其正常工作。如何在此平台特定的代码中扩展可访问性服务?

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 Client End调用此代码?因为据我所知,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() {
    }

...
}

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)

As you know, We must write the code inside MainActivity that extends FlutterActivity in order to make it work. How to extend AccessibilityService in this platform-specific code?

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? because as far as I know, accessibility service code is automatically called by the android system

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() {
    }

...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ㄖ落Θ余辉 2025-01-28 15:54:10

几天前,我制作了一个扑朔迷离的插件,以与Android中的可访问性服务进行交互。易于使用,只需按照读数中的说明进行操作,然后检查示例以获取更多详细信息
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
)
  */
  
  });

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

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
)
  */
  
  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文