The XML configuration for the custom accessibilityservice:
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" android:description="@string/accessibility_service_description" android:accessibilityEventTypes="typeAllMask" android:accessibilityFlags="flagDefault" android:accessibilityFeedbackType="feedbackGeneric" android:canRetrieveWindowContent="true" />
Where description is the description string for the accessibility that the user allows to apply, there is no application packagenames specified to be assisted, and when not specified, all apps are assisted by default. Typeallmask is the type of Set response event, Feedbackgeneric is the way to set the feedback to the user, with voice broadcast and vibration.
Registration of the custom Accessibilityservice in the mainfest manifest file:
<service android:name=".MyAccessibilityService" android:label="服务机器人" android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"> <intent-filter> <action android:name="android.accessibilityservice.AccessibilityService" /> </intent-filter> <meta-data android:name="android.accessibilityservice" android:resource="@xml/accessibility_service_config" /> </service>
Where label is the name that the user sees in the list of accessibility features, with the appropriate permissions and action added, and the configuration of Accessibilityservice in Meta-data.
Specific implementations in custom Accessibilityservice
When the accessibility of the app is started, there is a backend service that will monitor the phone interface for events, and perform some function callbacks, such as the onaccessibilityevent function, where the current window event is passed in. We need to determine the event type and resolve the layout in the window to get the controls in the specified layout.
For layout controls in the US app, we can use DDMS to get to, for example, the first page layout of the group:
After opening the app, we'll need to click on the "Search for business ..." control at the top of the search. So we need to specify when the page arrives, parse to the control, and click on the control (or its parent layout). Similar to other pages. (There are also some controls in the page that require input files).
We can do this "search Merchant ..." Control class is TextView, we can specify "type" and the class to search for the control.
public class Myaccessibilityservice extends Accessibilityservice {//The current event occurs with the package name String Nowpackagename; @TargetApi (build.version_codes. JELLY_BEAN_MR2) @Override public void Onaccessibilityevent (Accessibilityevent event) {//Gets the package name of the current event no Wpackagename = Event.getpackagename (). toString (); Determine if the current state is a running state if it is a nowpackagename.equals ("Com.sankuai.meituan") &&myapplication.getinstance () . Getflag ()) {//Determine if the desired window state changes if (Event.geteventtype () ==accessibilityevent.type_window_state_chang ED) {//Get event active window layout root node accessibilitynodeinfo rootNode = This.getrootinactivewindow (); Parse root node handle (RootNode); }}} @TargetApi (Build.version_codes. Jelly_bean) Private Boolean handle (Accessibilitynodeinfo info) {//determines if the node has child controls if (Info.getchildcount () = = 0) {//Determine if the node has text and have "search" text if (Info.gettext ()! = Null& &info.gettext (). ToString (). Contains ("search") {//Group's first page layout: Determine if the node is TextView, if the text matches if ("Search Merchant, category, or Business circle. Equals (Info.gettext (). toString ()) && "Android.widget.TextView". Equals (Info.getclassname ())) { Determines whether the node control is clickable, clicks its parent layout, and so on. Accessibilitynodeinfo parent = info; while (Parent!=null) {if (parent.isclickable ()) {//Analog click, jump out of loop Parent.performaction (Accessibilitynodeinfo.action_click); Break } parent = Parent.getparent (); }}//To judge the second layout: Determine if it is an Eidttext object else if ("search for a merchant, category, or Business Circle". Equals (Info.gettext (). toString ()) && "Android.widget.EditText". Equals (Info.getclassname ())) {//Enter text in the form of a clipboard C Lipboardmanager Clipboardmanager = (clipboardmanager) getsystemservice (CLIpboard_service); Clipdata clipdata =clipdata.newplaintext ("SCB", Myapplication.getinstance (). Getparams ()); Clipboardmanager.setprimaryclip (Clipdata); Simulated paste info.performaction (accessibilitynodeinfo.action_paste); The third layout is judged by whether it is a textview else if the word "search". Equals (Info.gettext (). toString ()) && "Android.wi Dget. TextView ". Equals (Info.getclassname ())) {//control click on the same principle accessibilitynodeinfo parent = info; while (Parent!=null) {if (parent.isclickable ()) {parent.performaction (Acc Essibilitynodeinfo.action_click); Break } parent = Parent.getparent (); }//After the service is finished, set flag myapplication.getinstance (). Setflag (false); Return TruE }else {//other conditions Set flag myapplication.getinstance (). Setflag (false); }}}} else {//When the current node has child controls, the child that resolves it, with this recursive for (int i = 0; i < Info.getchildcount (); i++) {if (Info.getchild (i)!=null) {Handle (Info.getchild (i)); }}} return false; } @Override public void Oninterrupt () {Toast.maketext (this, "ZZ", Toast.length_short). Show (); }}
As you can see, the above code is based on the logic of the American Regiment's search process.