Android availability com.android.settings: id / left_button for forced stop does not work

I am trying to use accessibility, I have access to accessibility.

But I want to use access to power to stop the application,

I'm trying to use

 event.getSource().findAccessibilityNodeInfosByViewId("com.android.settings:id/left_button")

On a sony mobile phone, this is the right job. But it does not work on an HTC cell phone.

How can I get the stop button identifier in each mobile phone? or other methods?

my code is below, thanks a lot

 private void forceStopApplication(AccessibilityEvent event) {  

    if (event.getSource() != null) {  
        if (event.getPackageName().equals("com.android.settings")) {  
            List<AccessibilityNodeInfo> stop_nodes = event.getSource().findAccessibilityNodeInfosByViewId("com.android.settings:id/left_button");  

            if (stop_nodes!=null && !stop_nodes.isEmpty()) {  
                AccessibilityNodeInfo node;  
                for(int i=0; i<stop_nodes.size(); i++){  
                    node = stop_nodes.get(i);  
                    if (node.getClassName().equals("android.widget.Button")) {  
                        if(node.isEnabled()){  
                           node.performAction(AccessibilityNodeInfo.ACTION_CLICK);  
                        } else {  
                            performGlobalAction(GLOBAL_ACTION_BACK);  
                        }  
                        node.recycle();  
                    }  
                }  
            }  

            List<AccessibilityNodeInfo> ok_nodes = null;  
            if(event.getText() != null && event.getText().size() == 4) {  
                ok_nodes = event.getSource().findAccessibilityNodeInfosByText(event.getText().get(3).toString());  
            }  
            if (ok_nodes!=null && !ok_nodes.isEmpty()) {  
                AccessibilityNodeInfo node;  
                for(int i=0; i<ok_nodes.size(); i++){  
                    node = ok_nodes.get(i);  
                    if (node.getClassName().equals("android.widget.Button")) {  
                        node.performAction(AccessibilityNodeInfo.ACTION_CLICK);  
                        Log.d("action", "click ok");  
                    }  
                    node.recycle();  
                }  
            }  
        }  
    }  
}  
+4
source share
2 answers

You can create a list of everyone AccessibilityNodeInfoand find a node that has a "Force Stop" as text using the method node.getText().

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

switch (eventType) {

    case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:

        AccessibilityNodeInfo rootNode = getRootInActiveWindow();
        ArrayList<AccessibilityNodeInfo> buttonNodes = new ArrayList<AccessibilityNodeInfo>();

        findChildViews(rootNode);

        for(AccessibilityNodeInfo mNode : buttonNodes){
            if(mNode.getText()==null){
                return;
            }
            if(mNode.getText().toString().contentEquals("FORCE STOP"){
                mNode.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            }

        }
        break;

 }
}

Method findChildViews():

private void findChildViews(AccessibilityNodeInfo parentView) {
        if (parentView == null || parentView.getClassName() == null || ) {
            return;
        }

        if (childCount == 0 && (parentView.getClassName().toString().contentEquals("android.widget.Button"))) {
            buttonNodes.add(parentView);
        } else {
            for (int i = 0; i < childCount; i++) {
                findChildViews(parentView.getChild(i));
            }
        }
    }
}

; .

+1

HTC : force_stop_button, left_button

+1

All Articles