Search code examples
androidaccessibilityservice

Force stop app using accessibility service Android?


I am using accessibility service for reading top package. But now I need to stop app and stop it's all running services through accessibility. I have been searching on same found clean master doing same. Please suggest how can I do same?


Solution

  • Using accessibility service you can force stop application.Ref below code

        public class MyAccessibilityService extends AccessibilityService {
    
        @Override
        public void onAccessibilityEvent(AccessibilityEvent event) {
           //TYPE_WINDOW_STATE_CHANGED == 32
            if (AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED == event
                    .getEventType()) {
                AccessibilityNodeInfo nodeInfo = event.getSource();
    
                if (nodeInfo == null) {
                    return;
                }
    
                List<AccessibilityNodeInfo> list = nodeInfo
                        .findAccessibilityNodeInfosByViewId("com.android.settings:id/left_button");
            //We can find button using button name or button id
                for (AccessibilityNodeInfo node : list) {
                    node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                }
    
                list = nodeInfo
                        .findAccessibilityNodeInfosByViewId("android:id/button1");
                for (AccessibilityNodeInfo node : list) {
                    node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                }
            }
    
        }
    
        @Override
        public void onInterrupt() {
            // TODO Auto-generated method stub
    
        }
       }
    

    Open Setting screen click application which u want to force stop from installed application.

    https://developer.android.com/guide/topics/ui/accessibility/services.html#event-details