I'm new member.
I found out the way to filter AccessibilityService is packageNames use getServiceInfo and setServiceInfo.
I want to filter all Active packages to know what app is open. That mean maybe it about 50 150 packages in list.
Could you help me the best case for CPU , resource saving ( which it better ?) 1/ leave default packageNames is null and filter in onAccessibilityEvent 2/ set packageNames is list 50 150 packages
PS: When I leave it default, many event happen ... so that afraid It eat many battery
Thanks
These events happen, whether you add the package names or not. If you look deep into the Android Open Source code, you will see that most of the steps of building these events that occur to "drain battery" occur regardless of whether or not you filter them. The filtering is for your convenience, so that you don't end up filtering them in Java as a first step in your onAccessibilityEvent
method. Do what makes the most sense for your application and use case, it will have minimal performance impact UNLESS, your Java filtering function that serves similar purposes is broken.
The BEST CASE for CPU savings, is for you to make the handling of the events that you care about as efficient as possible, and to have no expensive operations take place on these events.
Secondly, is to let as few events make it into your business logic as serves your use case. HOWEVER, how you filter these events is inconsequential. If you leave this configuration default, filter them out through Java code. If you can accomplish all of your filtering by setting up some of the filters that Service Configuration allows (package filters, throttling, etc) go for it. Or a combination of both approaches. But, there is minimal to zero performance advantage to one approach over the other.
Overall: prefer clean code and only handling events that you care about. In particular throttling/filtering as many scroll and screen change related events as possible. However, overthinking how this filtering occurs is a waste of time.