Learn powerful accessibilityservice!!!
Below is the core code for micro-letter automatic greeting to nearby people, based on the idea of automatic robbery of red envelopes
public class Autoservice extends Accessibilityservice implements View.onclicklistener {private static final String TAG
= "Test";
/** * Micro-Letter Package name */static final String wechat_packagename = "com.tencent.mm";
/** * Push message in the notification bar keyword, set to push account name, such as "10 reading"/static final String Push_text_key = "10 reading";
/** * Push link keyword, all push link headings need to include this keyword: such as "deep good text"/private static final String Url_text_key = "deep good text";
/** * Automatic Greeting to nearby people/private String hello = "test app automatic greeting function, this is a test information";
Boolean startFunc2 = false;//tag turns on the ability to automatically add a nearby person as a friend;
int i = 0;//records the number of people who have greeted int page=1;//records the person list page number, the initial page number is 1 int prepos = -1;//Record page jump source, 0--from the nearby person page jumps to the detail page, 1--from the greeting page jumps to the detailed information page
@Override public void Onaccessibilityevent (final accessibilityevent event) {int eventtype = Event.geteventtype (); Notification Bar Event//Automatically open push link if (EventType = = accessibilityevent.type_notification_state_changed) {list<charsequence> te
XTS = Event.gettext (); if (!texts.isempty ()) {for (charsequence t:texts) {String Text = String.valueoF (t);
if (Text.contains (Push_text_key)) {opennotification (event);
Opendelay (1000, url_text_key);
(}}}}///Automatically add person if (!STARTFUNC2) {return; } if (EventType = accessibilityevent.type_window_state_changed && event.getclassname (). Equals ("
Com.tencent.mm.ui.LauncherUI ")) {//Record greeting number set 0 i = 0;
At present on the micro-letter Chat page on the point opened found Opennext ("discovery");
Then jump to a nearby person Opendelay (1000, "near the person"); else if (Event.getclassname (). Equals ("Com.tencent.mm.plugin.nearby.ui.NearbyFriendsUI") && EventType = =
accessibilityevent.type_window_state_changed) {prepos = 0;
Currently in the vicinity of the human interface on the point of selecting people to greet accessibilitynodeinfo NodeInfo = Getrootinactivewindow ();
list<accessibilitynodeinfo> list = Nodeinfo.findaccessibilitynodeinfosbytext ("within meters");
LOG.D ("name", "number of people near the list:" + list.size ());
if (I < (list.size () *page)) {List.get (I%list.size ()). Performaction (Accessibilitynodeinfo.action_click);
List.get (I%list.size ()). GetParent (). Performaction (Accessibilitynodeinfo.action_click);else if (i = = List.size () *page) {//This page has all greeted, so the slide list loads the next page, each slide is a screen for (int i = 0; i < nodeinfo.getchild (0). getc Hildcount (); i++) {if (Nodeinfo.getchild (0). Getchild (i) getclassname (). Equals ("Android.widget.ListView")) {Accessibilitynodein
Fo NODE_LSV = nodeinfo.getchild (0). Getchild (i);
Node_lsv.performaction (Accessibilitynodeinfo.action_scroll_forward);
page++;
New Thread (New Runnable () {@Override public void run () {try {thread.sleep (1000);
catch (Interruptedexception ME) {me.printstacktrace ();
} accessibilitynodeinfo Nodeinfo_ = Getrootinactivewindow ();
list<accessibilitynodeinfo> list_ = Nodeinfo_.findaccessibilitynodeinfosbytext ("within meters");
LOG.D ("name", "List Number:" +list_.size ());
After sliding, the last item on the previous page is the current first item, so greet List_.get (1) from the second start. Performaction (Accessibilitynodeinfo.action_click);
List_.get (1). GetParent (). Performaction (Accessibilitynodeinfo.action_click);
}). Start (); (}}}} ElSe if (event.getclassname (). Equals ("Com.tencent.mm.plugin.profile.ui.ContactInfoUI") && EventType = = accessibilityevent.type_window_state_changed) {if (Prepos = = 1) {//from the greeting interface jumps to, then click to return to the nearby Person page performglobalaction (Acc
Essibilityservice.global_action_back);
i++;
If else if (Prepos = 0) {//jump from nearby people, click the Hello button accessibilitynodeinfo nodeinfo = Getrootinactivewindow ();
if (NodeInfo = null) {LOG.W (TAG, "Rootwindow is empty");
Return
} list<accessibilitynodeinfo> List = Nodeinfo.findaccessibilitynodeinfosbytext ("Hello");
if (list.size () > 0) {list.get (List.size ()-1). Performaction (Accessibilitynodeinfo.action_click);
List.get (List.size ()-1). GetParent (). Performaction (Accessibilitynodeinfo.action_click); else {//if the "Hello" to the interface that has been added as a friend changes to "send a message", return directly to the previous interface and record the number of greeting +1 performglobalaction (accessibilityservice.global_action
_back);
i++; else if (Event.getclassname (). Equals ("Com.tencent.mm.ui.contact.SayHiEditUI") && EventType = = accessibilityevent.type_window_state_changed) {//Current on the greeting page prepos = 1;
Enter the content of the greeting and send Inputhello (hello);
Opennext ("send"); //Automatically open the micro-letter from the desktop, using the micro-letter multiple open assistant to achieve multiple micro-letter account switching between//if (Topactivity.equals ("Com.huawei.android.launcher.Launcher")) {//Opennext
(Event, "micro-letter");
Accessibilitynodeinfo nodeinfo = Getrootinactivewindow ();
Nodeinfo.getchildcount (); for (int i=0;i<nodeinfo.getchildcount (); i++) {//String name=nodeinfo.getchild (i) getviewidresourcename ();//}/ /}/** * Open notification BAR message * * private void Opennotification (Accessibilityevent event) {if (event.getparcelabledata () = n ull | | ! (Event.getparcelabledata () instanceof Notification))
{return;
//Below is the essence of the micro-letter notification Bar message open Notification Notification = (Notification) event.getparcelabledata ();
Pendingintent pendingintent = notification.contentintent;
try {pendingintent.send ();
catch (Pendingintent.canceledexception e) {e.printstacktrace (); }/** * Click on the matching nodeinfo * @param str text keyword * * private void OPennext (String str) {Accessibilitynodeinfo nodeinfo = Getrootinactivewindow ();
if (NodeInfo = null) {LOG.W (TAG, "Rootwindow is empty");
Return
} list<accessibilitynodeinfo> List = Nodeinfo.findaccessibilitynodeinfosbytext (str);
LOG.D ("name", "Match number:" + list.size ());
if (list.size () > 0) {list.get (List.size ()-1). Performaction (Accessibilitynodeinfo.action_click);
List.get (List.size ()-1). GetParent (). Performaction (Accessibilitynodeinfo.action_click); }//if ("Com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyReceiveUI". Equals (Event.getclassname ())) {////The red envelopes in the point,
The next step is to remove the red envelopes//CheckKey1 (); else if ("Com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyDetailUI". Equals (Event.getclassname ())} {//// Look at the details of the record screen after the packet is removed/} else if ("Com.tencent.mm.ui.LauncherUI". Equals (Event.getclassname ())) {////In Chat interface, go to point in red envelopes//CheckKey2
(); }//Delay Open Interface private void Opendelay (final int delaytime, final String text) {New Thread (new Runnable () {@Overr
IDE public void Run () {try {thread.sleep (delaytime);
catch (Interruptedexception ME) {me.printstacktrace ();
} opennext (text);
}). Start ();
}//Automatically enter Hello content private void Inputhello (String hello) {accessibilitynodeinfo nodeinfo = Getrootinactivewindow ();
Locate the view accessibilitynodeinfo target = Nodeinfo.findfocus (Accessibilitynodeinfo.focus_input) that is currently gaining focus;
if (target = = null) {LOG.D (TAG, "inputhello:null");
Return
} clipboardmanager Clipboard = (Clipboardmanager) getsystemservice (Clipboard_service);
Clipdata clip = clipdata.newplaintext ("label", hello);
Clipboard.setprimaryclip (clip); if (Build.VERSION.SDK_INT >= build.version_codes.
JELLY_BEAN_MR2) {target.performaction (accessibilitynodeinfo.action_paste);
@Override public void Oninterrupt () {Toast.maketext (this, service interrupted, Toast.length_short). Show ();
} @Override protected void onserviceconnected () {super.onserviceconnected ();
Toast.maketext (This, "Connection service", Toast.length_short). Show (); } @Override PublIC void OnCreate () {super.oncreate ();
Createfloatview ();
} WindowManager WM;
Button floatbtn;
Create a hover button private void Createfloatview () {windowmanager.layoutparams PL = new Windowmanager.layoutparams (); WM = (WindowManager) getsystemservice (Getapplication ().
Window_service);
Pl.type = WindowManager.LayoutParams.TYPE_PHONE;
Pl.format = pixelformat.rgba_8888;
Pl.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; pl.gravity = Gravity.right |
Gravity.bottom;
pl.x = 0;
PL.Y = 0;
Pl.width = 200;
Pl.height = 200;
Layoutinflater Inflater = Layoutinflater.from (this);
FLOATBTN = (Button) inflater.inflate (r.layout.floatbtn, NULL);
Wm.addview (floatbtn, PL);
Floatbtn.setonclicklistener (this);
@Override public void OnClick (View v) {if (STARTFUNC2) {Floatbtn.settext ("Enable add Person");
else {floatbtn.settext ("stop adding people");
} STARTFUNC2 =!startfunc2;
}
}
To enable the code for this service:
Open System setup with accessibility
Intent Intent = new Intent (Android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
StartActivity (Intent);
To add a permission:
<uses-permission android:name= "Android.permission.BIND_ACCESSIBILITY_SERVICE"/>
Demo Address: Click on the Open link
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.