Android supports automatic red packet snatching, and android supports red packet snatching.
The service of automatic snatching of red packets is simply implemented. The principle is to find the corresponding View based on the keyword and then click it automatically. The AccessibilityService is mainly used as an auxiliary service, which can basically meet the function of automatic snatching of red packets. However, some logic needs to be optimized. For example, after a red packet is split, you must manually click the return key, in order to perform the next automatic snatching of red packets.
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" android:packageNames="com.tencent.mm" />
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/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" 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 = "enable secondary service"/> </RelativeLayout>
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 {// enable the auxiliary function Intent intent = new Intent (android. provider. settings. ACTION_ACCESSIBILITY_SETTINGS); startActivity (intent); Toast. makeText (MainActivity. this, "find the red envelope, and then enable the service", 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 will // automatically handle clicks on the Home/Up button, so long // as you 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. access Ibility. accessibilityNodeInfo; import android. widget. toast; import java. util. list; /*** <p> Created by Administrator </p> * <p/> * hongbao plug-in service */public class EnvelopeService extends AccessibilityService {static final String TAG = "Jackie "; /*** package name */static final String WECHAT_PACKAGENAME = "com. tencent. mm ";/*** keyword of the red packet message */static final String ENVELOPE_TEXT_KEY =" [red packet] "; Handler handler = new Handler (); @ Override public void onAccessibilityEvent (AccessibilityEvent event) {final int eventType = event. getEventType (); Log. d (TAG, "event ---->" + event); // if (eventType = AccessibilityEvent. TYPE_NOTIFICATION_STATE_CHANGED) {List <CharSequence> texts = event. getText (); 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 the red packet snatching service", Toast. LENGTH_SHORT ). show () ;}@ Override protected void onServiceConnected () {super. onServiceConnected (); Toast. makeText (this, "connect to the red packet snatching service", Toast. LENGTH_SHORT ). show ();} private void sendNotificationEvent () {AccessibilityManager manager = (AccessibilityManager) getSystemService (ACCESSIBILITY_SERVICE); if (! Manager. isEnabled () {return;} AccessibilityEvent = AccessibilityEvent. obtain (AccessibilityEvent. type_icationication_state_changed); event. setPackageName (WECHAT_PACKAGENAME); event. setClassName (Notification. class. getName (); CharSequence tickerText = ENVELOPE_TEXT_KEY; event. getText (). add (tickerText); manager. sendAccessibilityEvent (event);}/*** open notification bar message */@ TargetApi (Build. VERSION_CODES.JEL LY_BEAN) private void openNotification (AccessibilityEvent event) {if (event. getParcelableData () = null |! (Event. getParcelableData () instanceof Notification) {return;} // The following is the essence. Open Notification notification = (Notification) event for the Notification bar message. getParcelableData (); PendingIntent pendingIntent = notification. contentIntent; try {pendingIntent. send ();} catch (PendingIntent. canceledException e) {e. printStackTrace () ;}@ TargetApi (Build. VERSION_CODES.JELLY_BEAN) private void openEnvelope (AccessibilityEvent eve Nt) {if ("com. tencent. mm. plugin. luckymoney. ui. luckyMoneyReceiveUI ". equals (event. getClassName () {// click the red packet. The next step is to remove the red packet checkKey1 ();} else if ("com. tencent. mm. plugin. luckymoney. ui. luckyMoneyDetailUI ". equals (event. getClassName () {// after the red packet is split, view the detailed record interface // nonething} else if ("com. tencent. mm. ui. launcherUI ". equals (event. getClassName () {// go to the red packet 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 packet"); for (AccessibilityNodeInfo n: list) {n. encryption maction (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 ("get red packet"); if (list. isEmpty () {list = nodeInfo. findAccessibilityNodeInfosByText (ENVELOPE_TEXT_KEY); for (AccessibilityNodeInfo n: list) {Log. I (TAG, "--> red packet:" + n); n. encryption maction (AccessibilityNode Info. ACTION_CLICK); break;} else {// The latest red packet to receive for (int I = list. size ()-1; I> = 0; I --) {AccessibilityNodeInfo parent = list. get (I ). getParent (); Log. I (TAG, "--> get red packet:" + parent); if (parent! = Null) {parent. Signature maction (AccessibilityNodeInfo. ACTION_CLICK); break ;}}}}}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.