Android implementation of micro-letter automatic red Envelope program _android

Source: Internet
Author: User

The simple realization of the micro-letter automatically Rob Red Envelopes service, the principle is based on the keyword to find the corresponding view, and then automatically click. Mainly used to accessibilityservice this ancillary services, the basic can meet the function of automatic red envelopes, but some logic needs to be optimized, for example, after a red envelope, you must manually click the return key to the next automatic grab red envelopes.

Androidmanifest.xml

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/" Android "package=" Com.jackie.webchatenvelope "> <application android:allowbackup=" true "android:icon=" @mipmap /ic_launcher "android:label=" @string/app_name "android:theme=" @style/apptheme "> <activity android:name=". Mainactivity "android:label=" @string/app_name "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </ intent-filter> </activity> <service android:enabled= "true" android:exported= "true" android:label= "@ String/app_name "Android:name=". 
Envelopeservice "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/envelope_service_config"/> </service> </application> </manifest>  

Envelope_service_config.xml

<?xml version= "1.0" encoding= "Utf-8"?> <accessibility-service xmlns:android= 
"http://" Schemas.android.com/apk/res/android " 
android:accessibilityeventtypes=" typenotificationstatechanged| Typewindowstatechanged " 
android:accessibilityfeedbacktype=" Feedbackgeneric " 
android:accessibilityflags = "" 
android:canretrievewindowcontent= "true" 
android:description= "@string/accessibility_description" 
android:notificationtimeout= "100" 

Activity_main.xml

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" 
xmlns:tools= "http:// Schemas.android.com/tools " 
android:layout_width=" match_parent " 
android:layout_height=" Match_parent " 
android:paddingbottom= "@dimen/activity_vertical_margin" 
android:paddingleft= "@dimen/activity_ Horizontal_margin " 
android:paddingright=" @dimen/activity_horizontal_margin " 
android:paddingtop=" @dimen /activity_vertical_margin " 
tools:context=". Mainactivity "> 
<button 
android:id=" @+id/start " 
android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" 
android:padding= "10DP" 
android:layout_centerinparent= "true" 
android:textsize= "18SP" 
android:text= "open secondary service"/> 

Mainactivity.java

Package com.jackie.webchatenvelope; 
Import android.app.Activity; 
Import android.content.Intent; 
Import Android.os.Bundle; 
Import Android.view.Menu; 
Import Android.view.MenuItem; 
Import Android.view.View; 
Import Android.widget.Button; 
Import Android.widget.Toast; 
public class Mainactivity extends activity {private Button startbtn; 
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
Setcontentview (R.layout.activity_main); 
STARTBTN = (Button) Findviewbyid (R.id.start); 
Startbtn.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {try {//Open system settings accessibility 
Intent Intent = new Intent (Android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS); 
StartActivity (Intent); 
Toast.maketext (Mainactivity.this, "Find a red envelope, then open the service can", Toast.length_long). Show (); 
catch (Exception e) {e.printstacktrace (); 
} 
} 
}); @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; this adds ITEMs to the Action Bar if it is present. 
Getmenuinflater (). Inflate (R.menu.menu_main, menu); 
return true; @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here.  The action bar would//automatically handle clicks on the Home/up button, so long//as your specify a parent activity in 
androidmanifest.xml. int id = item.getitemid (); 
Noinspection simplifiableifstatement if (id = = r.id.action_settings) {return true; 
return super.onoptionsitemselected (item);  } 
}

Envelopeservice.java

Package com.jackie.webchatenvelope; 
Import Android.accessibilityservice.AccessibilityService; 
Import Android.annotation.TargetApi; 
Import android.app.Notification; 
Import android.app.PendingIntent; 
Import Android.os.Build; 
Import Android.os.Handler; 
Import Android.util.Log; 
Import android.view.accessibility.AccessibilityEvent; 
Import Android.view.accessibility.AccessibilityManager; 
Import Android.view.accessibility.AccessibilityNodeInfo; 
Import Android.widget.Toast; 
Import java.util.List; /** * <p>created by administrator</p> * <p/> * Rob red Envelopes Outside Service * * public class Envelopeservice extends Acce 
Ssibilityservice {static final String TAG = "Jackie"; 
/** * Micro-Letter Package name */static final String wechat_packagename = "com.tencent.mm"; 
/** * The keyword of the red envelope message * * Static final String Envelope_text_key = "[Micro-letter red envelope]"; 
Handler Handler = new Handler (); 
@Override public void Onaccessibilityevent (Accessibilityevent event) {final int eventtype = Event.geteventtype (); LOG.D (TAG,"Events---->" + event); Notification Bar Event if (EventType = = accessibilityevent.type_notification_state_changed) {list<charsequence> texts = EVENT.G 
Ettext (); 
if (!texts.isempty ()) {for (charsequence t:texts) {String text = string.valueof (t); 
if (Text.contains (Envelope_text_key)) {opennotification (event); 
Break 
else if (EventType = = accessibilityevent.type_window_state_changed) {Openenvelope (event); 
}/* @Override protected Boolean onkeyevent (KeyEvent event) {//return super.onkeyevent (event); 
return true; 
}*/@Override public void Oninterrupt () {Toast.maketext (this, "Interrupt robbery Service", Toast.length_short). Show (); 
} @Override protected void onserviceconnected () {super.onserviceconnected (); 
Toast.maketext (This, "link to rob Red Envelopes Service", Toast.length_short). Show (); private void Sendnotificationevent () {Accessibilitymanager manager = (Accessibilitymanager) getsystemservice (ACCESSI 
Bility_service); 
if (!manager.isenabled ()) {return; } accessibilityevent event = AcCessibilityevent.obtain (accessibilityevent.type_notification_state_changed); 
Event.setpackagename (Wechat_packagename); 
Event.setclassname (Notification.class.getName ()); 
Charsequence tickertext = Envelope_text_key; 
Event.gettext (). Add (Tickertext); 
Manager.sendaccessibilityevent (event); /** * Open the Notification bar message/@TargetApi (build.version_codes. Jelly_bean) private void Opennotification (Accessibilityevent event) {if (event.getparcelabledata () = NULL | |! ( 
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 (); }} @TargetApi (Build.version_codes. Jelly_bean) private void Openenvelope (Accessibilityevent event) {if (" Com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyReceiveUI ". Equals (Event.getclassname ())) {//point in the red envelopes, the next step is to remove the red envelope cheCkKey1 (); 
else if ("Com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyDetailUI". Equals (Event.getclassname ())) {//After the red envelope is removed, look at the detailed record interface 
Nonething} else if ("Com.tencent.mm.ui.LauncherUI". Equals (Event.getclassname ()) {//In the chat interface, to the point of the red Envelope checkKey2 (); }} @TargetApi (Build.version_codes. 
Jelly_bean) private void CheckKey1 () {Accessibilitynodeinfo nodeinfo = Getrootinactivewindow (); 
if (NodeInfo = null) {LOG.W (TAG, "Rootwindow is empty"); 
Return 
} list<accessibilitynodeinfo> List = Nodeinfo.findaccessibilitynodeinfosbytext ("Remove red envelopes"); 
for (Accessibilitynodeinfo n:list) {n.performaction (Accessibilitynodeinfo.action_click); }} @TargetApi (Build.version_codes. 
Jelly_bean) private void CheckKey2 () {Accessibilitynodeinfo nodeinfo = Getrootinactivewindow (); 
if (NodeInfo = null) {LOG.W (TAG, "Rootwindow is empty"); 
Return 
} list<accessibilitynodeinfo> List = Nodeinfo.findaccessibilitynodeinfosbytext ("Collect red envelopes"); if (List.isEmpty ()) {list = Nodeinfo.findaccessibilitynodeinfosbyteXT (Envelope_text_key); 
for (Accessibilitynodeinfo n:list) {log.i (TAG, "--> micro-letter Red Envelope:" + N); 
N.performaction (Accessibilitynodeinfo.action_click); 
Break } else {//latest Red envelope collar for (int i = List.size ()-1; I >= 0; i--) {Accessibilitynodeinfo parent = List.get (i). Getpar 
ENT (); 
LOG.I (TAG, "--> Collect Red Envelopes:" + parent); 
if (parent!= null) {parent.performaction (Accessibilitynodeinfo.action_click); 
Break  } 
} 
} 
} 
}

The above is for Android to achieve micro-letter automatic red envelope procedures, I hope to help.

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.