Android Accessibility service
I accidentally saw this article. This article describes how to use the auxiliary function service to simulate clicks.
Therefore, you must first understand the Auxiliary Function service, Accessibility service. The description and usage of AccessibilityService on the Internet have been described in a lot of detail. If you can go over the wall and have no problem with English, go to the official website: Workshop.
Here we will mainly describe the implementation principle of "simulated click.
First, use the AccessibilityEvent. getSource () method added in API 14, the AccessibilityNodeInfo. findAccessibilityNodeInfosByViewId () method added in API 18, and the AccessibilityNodeInfo. Signature maction () method;
Then, the AccessibilityEvent. getSource () method can be used to obtain the content and behavior of the window from the resource. The AccessibilityNodeInfo method can be used to determine the button to be clicked through the findAccessibilityNodeInfosByViewId () method;
After studying the source code, the view id corresponding to Force stop is R. id. left_button and findAccessibilityNodeInfosByViewId ("com. android. settings: id/left_button ");
After confirming the button to be clicked, click AccessibilityNodeInfo. Signature maction (AccessibilityNodeInfo. ACTION_CLICK ).
In this step, we have implemented the following: Click force stop, but after clicking force stop, a dialog box for us to confirm is displayed, so we need to click OK.
The view id of the confirmation button is button1, but you do not know why the confirmation button cannot be found using the findAccessibilityNodeInfosByViewId () method. AlertDialog may be somewhat special. I finally thought that the position of the confirmation button in the Dialog is fixed, so the AccessibilityEvent is used. getText (). get (3) to get the text value of the confirmation button, and then call AccessibilityNodeInfo. findAccessibilityNodeInfosByText (String text), and finally click the AccessibilityNodeInfo event. encryption maction (AccessibilityNodeInfo. ACTION_CLICK ).
The following code is used:
@SuppressLint(NewApi)private void processKillApplication(AccessibilityEvent event) {//Log.d(ATM, event.getEventType()+++);if (event.getSource() != null) {if (event.getPackageName().equals(com.android.settings)) {List stop_nodes = event.getSource().findAccessibilityNodeInfosByViewId(com.android.settings:id/left_button);//Log.d(ATM, stop_nodes.toString());if (stop_nodes!=null && !stop_nodes.isEmpty()) {AccessibilityNodeInfo node;for(int i=0; i
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