如何在平台特定的平台中使用可访问性服务
我正在尝试将可访问性服务集成到基于颤音的应用程序中,并挖掘出来,发现我必须使用方法频道来实现此任务,因为仅通过使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
几天前,我制作了一个扑朔迷离的插件,以与Android中的可访问性服务进行交互。易于使用,只需按照读数中的说明进行操作,然后检查示例以获取更多详细信息
flutter_accessibility_service
这是如何使用它的示例
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