Share An Android code example for automatically snatching red packets on WeChat

Source: Internet
Author: User
Code snippets, code sharing, PHP code sharing, Java code sharing, Ruby code sharing, Python code sharing, HTML code sharing, CSS code sharing, SQL code sharing, javaScript code sharing provides a simple service for automatic snatching of red packets. 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
 
                          
                                   
               
                   
              
                               
               
           
       
 Envelope_service_config.xml
 Activity_main.xml
      
   
 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. accessibility. accessibilityNodeInfo; import android. widget. toast; import java. util. list ;/***

Created by Administrator

*

* Snatching 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_icationication_state_changed) {List 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 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 () {AccessibilityNodeI Nfo nodeInfo = getRootInActiveWindow (); if (nodeInfo = null) {Log. w (TAG, "rootWindow is empty"); return;} List 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 (AccessibilityNodeInfo. ACTION_CLICK); break;} else {// The latest red packet collar Start 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 ;}}}}}

The above is to share the details of an Android code instance for automatically snatching red packets. For more information, see other related articles on php Chinese network!

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.