Android Accessibility service

Source: Internet
Author: User

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
  
   

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.