0

I've got an AccessibilityEvent which can stop app automatically. But when starting app info intent, it goes to infinite loop of turn on/off alert dialog.

How can I prevent it ? Here's the code:

 @Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    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/force_stop_button");
                for (AccessibilityNodeInfo node : list) {
                    Log.i(TAG, "check1 = " + check);
                    node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    check = true;
                }

                list = nodeInfo
                        .findAccessibilityNodeInfosByText("CANCEL");
                for (AccessibilityNodeInfo node : list) {
                    node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                }
                }
            }

And by the way, any idea to get the force_stop_button clicked immediately after the app info intent started ?

EDIT: I think the problem is in AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED. If I bypass that check, it works, but really hard to control.

Chris Maverick
  • 928
  • 1
  • 8
  • 18

1 Answers1

2

In your code, whenever the AccessibilityService detects the Force Stop button in Settings activity, it is clicked. Now, a dialog will be shown with two buttons : Ok & Cancel. You are then performing click action on cancel button.

So, App Info -> Dialog -> Cancel -> App Info -> Dialog -> Cancel ...

is the loop you will get.

Change list = nodeInfo.findAccessibilityNodeInfosByText("CANCEL");

to list = nodeInfo.findAccessibilityNodeInfosByText("OK");

In order to click a node, you have to add some delay :

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
    }
}, 300);
Prasad Pawar
  • 1,606
  • 1
  • 15
  • 30
  • Actually that's not what I want to ask. Because it doesn't even click "Force Stop". So why should I care about clicking "Cancel" or "OK" ? – Chris Maverick Dec 14 '15 at 14:34
  • In my code now, actually there is a few "delayed" code. But it's very passive. First, if my phone's just been doing a really heavy job and I run this code, 300ms seems not enough. Second, I think the code should be delayed is "findnode", not the "performAction" one. But anyway, one upvote :D – Chris Maverick Dec 15 '15 at 07:09
  • Ya but i do think delay on performAction will works. Thanks anyway ;) – Prasad Pawar Dec 15 '15 at 07:35