0

I'm developing an app that launches over other apps. Currently I'm using an AccessibilityService with setServiceInfo to listen to window changes, for specific package names, but I'm unable to change these package names dynamically.

private void setServiceInfo(String apps[]) {

    AccessibilityServiceInfo info = new AccessibilityServiceInfo();
    info.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
    info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
    info.notificationTimeout = 0;
    info.packageNames = apps;
    setServiceInfo(info);
}

The above code is how I'm setting the serviceInfo. I'm calling setServiceInfo onServiceConnected to first initialise and onAccessibilityEvents for any changes to the package names.

If accessibility service isn't the way to go about this, please suggest alternatives such as services etc.

Chris Rohit Brendan
  • 893
  • 1
  • 9
  • 20

2 Answers2

0

So I had to work around my problem. Instead of setting the package names by calling setServiceInfo and changing the value of packages, I set the info.packageNames = null.

    AccessibilityServiceInfo info = new AccessibilityServiceInfo();
    info.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
    info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
    info.notificationTimeout = 0;
    info.packageNames = null;
    setServiceInfo(info);

This way onAccessiblityEvent was triggered for all packages, but I considered only specific packages by comparing package that called the event, with the specific packages that I needed using event.getPackageName().toString();

  @Override
        public void onAccessibilityEvent(AccessibilityEvent event) {
            final int eventType = event.getEventType();

            switch (eventType) {

                case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:

                    package = event.getPackageName().toString();
                    Toast.makeText(this, "Came to window state changed", Toast.LENGTH_SHORT).show();

                        if (whateverpackage.contains(newApp)) {

                        //Then do something 

         }

Just replace whateverpackage with the Package name that you require.

Chris Rohit Brendan
  • 893
  • 1
  • 9
  • 20
0

Reuse existent AccessibilityServiceInfo object via getServiceInfo() (added in API 16) instead of creating new one:

     @Override
     protected void onServiceConnected() {
            super.onServiceConnected();
            AccessibilityServiceInfo info = getServiceInfo();
            // your other assignments
            info.packageNames = apps;
            setServiceInfo(info);
        }

This worked in my tests.

stone
  • 274
  • 4
  • 8