'How to delete packages from packageNames in Accessibility service in android ?

I have Accessibiltiy service which opens my activity when specific apps start.

    public class Accessibility extends AccessibilityService{
  private AccessibilityServiceInfo info;
        @Override
        protected void onServiceConnected() {

            super.onServiceConnected();
            info = new AccessibilityServiceInfo();
            info.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED | AccessibilityEvent.TYPE_VIEW_CLICKED;

            info.packageNames = null;
            DatabaseHandler db = new DatabaseHandler(this);
            List<AppData> blackList = db.getAllApps();

            info.packageNames = new String[blackList.size()];
            int k = 0;
            for (AppData app : blackList) {
                if(app.isBlocked() == 1) {
                    info.packageNames[k] = app.getPackageName();
                    k++;
                }
            }
            for(int i = 0; i < info.packageNames.length; i++) {
                Log.d(myTag, "BLOCKED APP = " +   info.packageNames[i]);
            }
            info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
            info.notificationTimeout = 100;
            this.setServiceInfo(info);

            //register Broadcastreceiver
            registerReceiver(receiver, new IntentFilter("UPDATE_BLACKLIST"));   
        }

        //Receives notification when blacklist was updated
        private final BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                onServiceConnected();
            }
        };
    }

When accessibility service gets broadcast receiver, it calls method onServiceConnected() which has to update list with packageNames and it updates successfully but, if I update list again with new package names, my accessibiltiy service still continue to work when I open application which is not now in the list. I displayed in logs the full list - there is no old package names, but accessibiltiy service still works when I open that applications. How can I totally update package names in my service ?



Solution 1:[1]

Use info = getServiceInfo() instead of info = new AccessibilityServiceInfo();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Syscall