Android auxiliary function Accessibilityservice and red envelope assistant _android

Source: Internet
Author: User

Recommended reading: The principle analysis and development thinking of the plugin of micro-letter robbery in Android

The principle of stealing red envelopes are almost the same, usually using Android's accessibility (Accessibilityservice Class) to listen to the Notification Bar Event or window Change event to find the red envelope keyword and then to simulate the click or open red envelopes.

Attached below the source code, the program has realized automatic grab red envelopes, lock screen black screen state automatic Jianjiang screen, android4.x test passed. Function specific features see detailed comments.
Note: In the chat interface received red envelopes will not automatically open, because the notification bar no message prompts so that the monitoring can not, at this point only a manual click. Other unknown situation please use Logcat debugging, the source code already has the related debugging information. The software is for learning and entertainment only.

<pre style= "margin-top:0px; margin-bottom:0px; " ><span style= "font-family:arial, Helvetica, Sans-serif; Color:rgb (192, 192, 192); ></span><pre style= "margin-top:0px; margin-bottom:0px; " 
>import Java.util.Calendar; 
Import java.util.List; 
Import Android.accessibilityservice.AccessibilityService; 
Import Android.annotation.SuppressLint; 
Import Android.app.KeyguardManager; 
Import Android.app.KeyguardManager.KeyguardLock; 
Import android.app.Notification; 
Import android.app.PendingIntent; 
Import android.app.PendingIntent.CanceledException; 
Import Android.content.Context; 
Import Android.media.MediaPlayer; 
Import Android.os.PowerManager; 
Import Android.util.Log; 
Import android.view.accessibility.AccessibilityEvent; 
Import Android.view.accessibility.AccessibilityNodeInfo; 
Import Android.widget.Toast; public class Demo extends Accessibilityservice {private Boolean canget = false;//can click on the red envelope private Boolean Enablekeyguard = true;//default has screen lock//window state Private Static final int window_none = 0;
private static final int window_luckymoney_receiveui = 1;
private static final int window_luckymoney_detail = 2;
private static final int window_launcher = 3;
private static final int window_other =-1;
Current window private int mcurrentwindow = Window_none;
Lock screen, unlock related private keyguardmanager km;
Private Keyguardlock KL;
Wake up screen related private powermanager pm;
Private Powermanager.wakelock WL = null;
Play tips private MediaPlayer player;
public void PlaySound {Calendar cal = calendar.getinstance (); int hour = Cal.get (Calendar.hour_of_day);
Do not play cue sounds if (hour > 7 && Hour <) {Player.start ()}} at night Wake screen and unlock private void Wakeandunlock (Boolean unLock) {if (UnLock) {//If black screen state then wake screen if (!pm.isscreenon ()) {//Get Power Manager object, Acqu Ire_causes_wakeup This parameter can wake the screen from the black screen wl = Pm.newwakelock (Powermanager.screen_bright_wake_lock |
Powermanager.acquire_causes_wakeup, "bright");
Light up the screen wl.acquire ();
LOG.I ("Demo", "bright screen"); ///If the lock screen interface is unlocked, simply skip the lock screen if (km.inkeyguardrestrictedInputmode ()) {//Set unlock sign to determine whether the red envelope can be locked Enablekeyguard = false;//unlock kl.disablekeyguard ();
LOG.I ("Demo", "unlock");
} else {//If the lock was previously unlocked to restore the original if (!enablekeyguard) {//Lock screen Kl.reenablekeyguard ();
LOG.I ("Demo", "Add lock");
///If the screen has been awakened before, release it so that the screen does not keep illuminated steady if (WL!= null) {wl.release (); wl = null;
LOG.I ("Demo", "Turn off the Light"); Find node public accessibilitynodeinfo findnodeinfosbytext by text (Accessibilitynodeinfo nodeinfo, String text) {list<
accessibilitynodeinfo> list = Nodeinfo.findaccessibilitynodeinfosbytext (text);
if (list = = NULL | | list.isempty ()) {return null;} return List.get (0); //Analog Click event public void PerformClick (Accessibilitynodeinfo nodeinfo) {if (NodeInfo = = null) {return;} if (Nodeinfo.isclick
Able ()) {nodeinfo.performaction (accessibilitynodeinfo.action_click);} else {PerformClick (nodeinfo.getparent ());}} Impersonation return event public void Performback (Accessibilityservice service) {if (service = = null) {returns;} service.performglobalacti
On (Accessibilityservice.global_action_back); //Implement Accessibility @Override
public void Onaccessibilityevent (Accessibilityevent event) {int eventtype = Event.geteventtype ();
LOG.I ("Demo", Integer.tostring (EventType)); Switch (EventType) {//Step one: Listen for notification bar message case accessibilityevent.type_notification_state_changed:list<charsequence> 
texts = Event.gettext (); 
if (!texts.isempty ()) {for (charsequence text:texts) {String content = text.tostring ();
LOG.I ("Demo", "Text:" +content); Received a red envelope reminder if (content.contains ("[micro-letter Red envelope]") | | Content.contains ("[QQ red Envelope]") {//Analog open Notification Bar message if (event.getparcelabledata ()!= null && event.getparcelabledata () in Stanceof Notification) {//play the hint tone playSound (this);//If the micro-letter red envelopes are unlocked and automatically open, if the QQ red envelopes only prompt and jump to have a red envelope chat interface, has not realized QQ red envelopes automatically pick up the function if (
Content.contains ("[micro-letter Red Envelope]") Wakeandunlock (true);
LOG.I ("Demo", "Canget=true");
Canget = true; 
try {Notification Notification = (Notification) event.getparcelabledata (); 
Pendingintent pendingintent = notification.contentintent;
Pendingintent.send (); 
catch (Canceledexception e) {e.printstacktrace (); }} BReak;
}} break; Step Two: Monitor whether enter the micro-letter Red Envelope Message interface case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:String ClassName = Event.getclassname ().
ToString (); if (Classname.equals ("Com.tencent.mm.ui.LauncherUI")) {Mcurrentwindow = Window_launcher;//start Robbing red envelopes log.i ("demo", "Prepare to rob Red envelopes")
...");
GetPacket (); else if (classname.equals ("Com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyReceiveUI")) {Mcurrentwindow = Window_
Luckymoney_receiveui;
Start to open the red envelope log.i ("demo", "Open red envelopes");
Openpacket ();
Wakeandunlock (FALSE); else if (classname.equals ("Com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyDetailUI")) {Mcurrentwindow = Window_
Luckymoney_detail;
Return to facilitate the next collection of red Envelopes LOG.I ("Demo", "Return");
Performback (this);
else {Mcurrentwindow = Window_other;} break; Case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:if (Mcurrentwindow!= window_launcher) {//Not in Chat interface or chat list, do not handle return
;
} if (Canget) {getpacket ();} break; }//Find red Envelopes and click @SuppressLint ("Newapi") private void GetPacket () {Accessibilitynodeinfo nodeinfo = GetrootinactivewindoW (); 
if (NodeInfo = = null) {return;
//Find the Click event list<accessibilitynodeinfo> List = Nodeinfo.findaccessibilitynodeinfosbytext ("Collect red envelopes"); if (list!= null) {if (List.isEmpty ()) {log.i ("demp", "Pick list is empty");//Find red envelopes from the list of messages accessibilitynodeinfo node = FINDNODEINFOSB
Ytext (NodeInfo, "[Micro-letter Red envelope]");
if (node!= null) {Canget = true; performclick (node);}}
else {if (canget) {//The latest red envelopes lead accessibilitynodeinfo node = List.get (List.size ()-1); PerformClick (node);
LOG.I ("Demo", "Canget=false");
Canget = false; Open the Red envelope @SuppressLint ("Newapi") private void Openpacket () {Accessibilitynodeinfo nodeinfo = Getrootinactivewindo
W ();
if (NodeInfo = = null) {return;}
LOG.I ("Demo", "Find Open button ...");
Accessibilitynodeinfo TargetNode = null;
If the red envelope has been robbed to return directly to TargetNode = Findnodeinfosbytext (NodeInfo, "Look at everyone's luck"); if (TargetNode!= null) {performback (this); return////Find out the Red envelope button through the component name, but you can find it by component ID directly but you need to know the ID and the ID is easy to change with the version update, the old version of the micro-letter can be directly searched
Word Find button if (TargetNode = null) {LOG.I ("demo", "Open button ..."); for (int i = 0;I < Nodeinfo.getchildcount ();
i++) {accessibilitynodeinfo node = nodeinfo.getchild (i); if ("Android.widget.Button". Equals (Node.getclassname ())) {
TargetNode = node;
Break 
Jocha Find the Open button then simulate click if (TargetNode!= null) {final Accessibilitynodeinfo n = targetnode; PerformClick (n);} @Override public void Oninterrupt () {Toast.maketext (this, "Rob Red Envelopes Service Interrupted ~", Toast.length_long). Show (); @Override protect
Ed void onserviceconnected () {super.onserviceconnected ();
LOG.I ("Demo", "Open");
Gets the Power Manager Object pm= (PowerManager) Getsystemservice (Context.power_service);
Get the keyboard lock Manager Object km= (Keyguardmanager) Getsystemservice (Context.keyguard_service);
Initializes a keyboard lock manager Object KL = Km.newkeyguardlock ("UnLock");

Initialize Audio player = Mediaplayer.create (this, r.raw.songtip_m);
Toast.maketext (This, "_ has been opened to rob Red Envelopes Service _", Toast.length_long). Show ();
@Override public void OnDestroy () {Super.ondestroy ();
LOG.I ("Demo", "Close");
Wakeandunlock (FALSE);
Toast.maketext (This, "_ has been closed to rob Red envelopes Service _", Toast.length_long). Show (); }
}

Declaring related services and permissions in Androidmanifest.xml

<uses-permission android:name= "Android.permission.WAKE_LOCK"/> 
<uses-permission android:name= " Android.permission.DISABLE_KEYGUARD "/> 
<application 
android:allowbackup=" true " 
android:icon= "@drawable/ic_launcher" 
android:label= "@string/app_name" 
android:theme= "@style/apptheme" > 
< Pre name= "code" class= "HTML" ><service 
android:name= "Com.example.test.Demo" android:enabled= "true" Android:exported= "true" android:label= "@string/app_name" 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=" @layout/accessibility_config "/></service></ Application>

Accessibility_config.xml service configuration content as follows

<accessibility-service xmlns:android= "Http://schemas.android.com/apk/res/android" 
android: Accessibilityeventtypes= "Typenotificationstatechanged|typewindowstatechanged|typewindowcontentchanged" 
Android:accessibilityfeedbacktype= "Feedbackgeneric" 
android:accessibilityflags= "FlagDefault" 
android: Canretrievewindowcontent= "true" 
android:description= "@string/desc" 
android:notificationtimeout= "100" 

Where description for the description of the auxiliary function, packagenames for the monitoring of the package name, here only to monitor the micro-mail and QQ Accessibilityeventtypes

The above mentioned is for the Android auxiliary function Accessibilityservice and robs the red envelope the correlation knowledge, hoped has the help to everybody.

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.