Accessibilityservice Helper class Usage (alternative hooks in Android)

Source: Internet
Author: User

Say it in the front.

Why would you call Accessibilityservice an alternative hook in android? The thing about Windows is that the hook means being able to hear everything you want to listen to, and Accessibilityservice in Android can listen to some of the features we need.

Introduction to Usage

Accessibilityservice is an auxiliary class that can monitor the focus of our phone, window changes, button clicks and so on. To implement its service needs to be in the phone settings----accessibility in this area to find your own implementation of the auxiliary class, and then open it can be a series of our monitoring.

Instance

Now we are going to implement a feature that automatically completes the installation after the user clicks on the installation package, because in the system, after clicking on the installation package, a confirmation installation screen will pop up. All we need to do is listen to the installation window to bounce out and then simulate the user Click OK Action, and finally click Install Finish. This process J will need to use our accessibilityservice to operate.
I'll put the code in here and explain it slowly. First, we'll create a new class to inherit Accessibilityservice.

 Public  class windowaccessibilityseivice extends accessibilityservice{    FinalString TAG = WindowAccessibilitySeivice.class.getSimpleName (); String installpackge[] = {"Com.android.packageinstaller"};@Override    protected void onserviceconnected()    {Super. onserviceconnected ();//Use code to configure current service information        //Accessibilityserviceinfo info = new Accessibilityserviceinfo ();        //Info.packagenames = INSTALLPACKGE;//monitor the packet name of the filter        //Info.eventtypes = Accessibilityevent.types_all_mask;//monitor What behavior        //Info.feedbacktype = Accessibilityserviceinfo.feedback_spoken;//Feedback        //info.notificationtimeout = 100;//time of notification        //Setserviceinfo (info);}@SuppressLint("Newapi")Private void findandperformaction(String text) {//Find the button that contains the "install" text in the current window        if(Getrootinactivewindow () = =NULL)return;//Find the current node by textlist<accessibilitynodeinfo> nodes = Getrootinactivewindow (). Findaccessibilitynodeinfosbytext (text); for(inti =0; I < nodes.size (); i++) {accessibilitynodeinfo node = nodes.get (i);//Perform button click Behavior            if(Node.getclassname (). Equals ("Android.widget.Button") &&node.isenabled ()) {node.performaction (Accessibilitynodeinfo.action_click); }        }    }@SuppressLint("Newapi")@Override     Public void onaccessibilityevent(Accessibilityevent event) {if(Event.getsource ()! =NULL) {Findandperformaction ("Install"); Findandperformaction ("Next"); Findandperformaction ("Done"); }    }@Override     Public void Oninterrupt()    {    }}
Onaccessibilityevent

The most important part of this is onaccessibilityevent this callback function, when we register the listener event, when there is an event will notify us of this function, but must note that this function notification is asynchronous, of course, many friends will ask this is how to notify here? He is by accessibilitydelegate this proxy class, sent out, this class has a method sendaccessibilityevent can send events. And how does this class relate to our window? Here's an example. For example, our view class has a setaccessibilitydelegate this method, is it all clear? Then just call our find function to go to the current node to find the node information we need, by simulating the click event to install our software, of course, if the input box can also be simulated input oh.

onserviceconnected

In this callback function, we can configure the information of our current service, so I can see that the code above is well written. But remember that we can also configure the information of our service in our XML, here I am also in the XML configuration information. Create a new folder under our Res folder XML and create a new XML file Accessibility.xml inside.

<?xml version= "1.0" encoding= "Utf-8"?><accessibility-service   Xmlns:android  = "http://schemas.android.com/apk/res/android"  android:accessibilityeventtypes  =" Typeallmask " android:accessibilityfeedbacktype  = "Feedbackgeneric"  Span class= "Hljs-attribute" >android:canretrievewindowcontent  = "true"  android:description  = "@string/testaccessibility"  Span class= "Hljs-attribute" >android:packagenames  = "Com.android.packageinstaller"  android:notificationtimeout  = "all" / 

The need to remind here is the configuration of the time Packagenames this is the name of the package you want to listen to, here we need to listen to the installation of information, So the package name is Com.android.packageinstaller, you can have multiple separated by commas, accessibilityeventtypes This is the listener event.
Remember to configure our service in the Mainfiset list.

   <serviceandroid:name="Com.bobo.accessbility.WindowAccessibilitySeivice"  Android:description= "@string/testaccessibility"android:label="Accessibility" android:permission="Android.permission.BIND_ACCESSIBILITY_SERVICE" >                                                            <intent-filter>                <action android:name="Android.accessibilityservice.AccessibilityService" />             </intent-filter>            <meta-dataandroid:name="Android.accessibilityservice"Android:resource ="@xml/accessibility" />                                        </Service>

Written here is basically ready to install and use. If you need to see more demos, you can look at the Sdkdemo inside the com.example.android.apis.accessibility this bag.
This gives the current demo portal

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Accessibilityservice Helper Class usage (alternative hooks in Android)

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.