Search code examples
androidaccessibilityservice

Accessibilityservice can not close AlertDialog from USSD response when there are CANCEL,SEND buttons


I've build a class a extends AccessibilityService :

...
public void onAccessibilityEvent(AccessibilityEvent event) {
        String text = event.getText().toString();
        if (event.getClassName().equals("android.app.AlertDialog")) {
            performGlobalAction(GLOBAL_ACTION_BACK);
            Toast.makeText(this,
                    text,
                    Toast.LENGTH_SHORT).show();
            G.last_msg = text;
...

Everything is good when the response has only "OK" button .

BUT if the dialog has "CANCEL" , "SEND" buttons and a TextBox , I can get the content of the dialog but can't dismiss it.

Also when this dialog is visible I tried to click system back button and It did not close this dialog.

I just want to click CANCEL. How ?


Solution

  • FOUND IT

    ...
    public void onAccessibilityEvent(AccessibilityEvent event) {
            String text = event.getText().toString();
            if (event.getClassName().equals("android.app.AlertDialog")) {
                performGlobalAction(GLOBAL_ACTION_BACK);
    // start solution
                AccessibilityNodeInfo source = event.getSource(); // source event
                List<AccessibilityNodeInfo> list;
                list = source.findAccessibilityNodeInfosByText("CANCEL"); // find the cancel button
                for (AccessibilityNodeInfo node : list) {
                    node.performAction(AccessibilityNodeInfo.ACTION_CLICK); // click it
                }
    // end solution
                Toast.makeText(this,
                        text,
                        Toast.LENGTH_SHORT).show();
                G.last_msg = text;
    ...