X-SLAYER / flutter_accessibility_service

Flutter plugin for interacting with Accessibility Service in Android.
https://pub.dev/packages/flutter_accessibility_service
MIT License
33 stars 24 forks source link

Usage from a foreground service #12

Closed rena821 closed 9 months ago

rena821 commented 10 months ago

I am experimenting with using the flutter accessiblity service from a flutter foreground task. When I try to listen to the events in the foreground task then I get the following exception if the app is closed:

PlatformException(error, Attempt to invoke virtual method 'void android.app.Activity.startActivityForResult(android.content.Intent, int)' on a null object reference, null, java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Activity.startActivityForResult(android.content.Intent, int)' on a null object reference
        at slayer.accessibility.service.flutter_accessibility_service.FlutterAccessibilityServicePlugin.onMethodCall(FlutterAccessibilityServicePlugin.java:56)
        at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:258)

It is probably because the package is trying to use an Activity context to call startActivityForResult. But this context may not be available in a foreground service.

Is there a way to use it from a foreground service?

X-SLAYER commented 10 months ago

You should ensure that any permissions required by the accessibility service are requested within your app's main activity. Once the necessary permissions are granted, you can then perform the desired actions within the foreground service.

rena821 commented 10 months ago

All the required permissions are granted. I tested it and it works very well when I call the FlutterAccessibilityService.accessStream.listen((event) . But as soon as I call it from a foreground service I get the error mentioned above.

X-SLAYER commented 9 months ago

Can you try the latest version of the package