Teach you step-by-step implementation of Android micro-letter automatic robbery Red Envelopes _android

Source: Internet
Author: User
Tags gettext

This paper introduces the realization method of micro-credit automatic robbery of red envelopes, mainly realizes the following functions:

1. Automatically disassemble the red envelopes appearing on the screen

2. Automatically enter the chat interface and remove red envelopes when you receive the red envelope information when you are on the desktop or chat list

3. Log function, record the detailed log of red envelopes

Implementation principle

1. Use Accessibilityservice ancillary services to monitor screen content and realize the purpose of automatically removing red envelopes.

2. Using the Activeandroid database to simply record the red envelope log

3. Use preference to realize monitoring option record

Final interface

Grab the red Envelope core code

Accessibilityservice Configuration

android:accessibilityEventTypesSets the type of event that triggers a listening callback;

android:packageNamesSet up the listening application, where the micro-letter is listening, so fill in the micro-letter package namecom.tencent.mm

<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/accessibility_description"
 android: notificationtimeout= "android:packagenames="
 com.tencent.mm "android:settingsactivity="
 Com.oden.annotations.app.activity.ManActivity "/>

Declare in Androidmanifest.xml:

 <service
   android:name= ". App.service.HongbaoService_"
   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= "@xml/accessibility_service_config"/>
  </service>

Stealing red envelopes to implement code

Sent by the receiving system.AccessibilityEvent

 private static final String Get_red_packet = "Collect red envelopes";
 private static final String Check_red_packet = "View Red envelopes";
 private static final String red_packet_picked = "Hange, red envelope sent out";
 private static final String Red_packet_picked2 = "Luck";
 private static final String Red_packet_picked_detail = "red envelope details";
 private static final String Red_packet_save = "Saved change";

 private static final String red_packet_notification = "[Micro-letter red envelope]";
  @Override public void Onaccessibilityevent (Accessibilityevent event) {L.D ("RECEIVE event!");
   if (watchedflags = null) return; /* Detect notification message */if (!mmutex) {if (Watchedflags.get ("pref_watch_notification") && watchnotifications (event)) RET
   Urn
  if (Watchedflags.get ("Pref_watch_list") && watchlist (event)) return;

  } if (!watchedflags.get ("Pref_watch_chat")) return;
  This.rootnodeinfo = Event.getsource ();

  if (Rootnodeinfo = null) return;
  Mreceivenode = null;

  Munpacknode = null;

   Checknodeinfo (); /* If a red envelope has been received and has not been poked open/if (Mluckymoneyreceived &&!mluckymoneypicked && (mreceivenode!= null)) {Mmutex = true;
   Accessibilitynodeinfo Cellnode = Mreceivenode;
   Cellnode.getparent (). Performaction (Accessibilitynodeinfo.action_click);
   Mluckymoneyreceived = false;
   Mluckymoneypicked = true; L.D ("Opening!")
  "); /* If the stamp is not received/*/if (Mneedunpack && (munpacknode!= null)) {Accessibilitynodeinfo Cellnode = Munpacknod
   E
   Cellnode.performaction (Accessibilitynodeinfo.action_click);
   Mneedunpack = false; L.D ("Being picked up!")
  ");
   } if (Mneedback) {performglobalaction (global_action_back);
   Mmutex = false;
   Mneedback = false; L.D ("Coming back!")
   ");
    Total number and amount statistics if (Isgetmoney) {T.showshort (this, "Grab a red envelope:" + Gotmoney + "Yuan!");
    Totalmoney = Totalmoney + Gotmoney;
    totalsuccessnum++;
    Myprefs.totalmoney (). put (Totalmoney);
    Myprefs.successnum (). put (Totalsuccessnum);
    L.D ("Totalmoney:" + Totalmoney);
    L.D ("Totalsuccessnum:" + totalsuccessnum); Savetolog (HonGbaoinfo);
   Isgetmoney = false; }
  }
 }

detects node information for listening events

private void Checknodeinfo () {L.D ("checknodeinfo!");
   if (This.rootnodeinfo = null) return; /* Chat session window, traversal node matching "Receive red envelopes" and "View Red envelopes" * * list<accessibilitynodeinfo> nodes1 = this.findaccessibilitynodeinfosbytexts (
  This.rootnodeinfo, New string[]{get_red_packet, check_red_packet});
   if (!nodes1.isempty ()) {L.D ("!nodes1.isempty ()");
   Accessibilitynodeinfo TargetNode = Nodes1.get (Nodes1.size ()-1); if ("Android.widget.LinearLayout". Equals (Targetnode.getparent (). GetClassName ()))//Avoid interference caused by text to plug the invalid {if (This.signatu
     Re.generatesignature (TargetNode)) {mluckymoneyreceived = true;
     Mreceivenode = TargetNode;
    L.D ("Signature:" + this.signature.toString ());
   } else {L.D ("This is Text");
  } return;
    } list<accessibilitynodeinfo> nodes2 = This.findaccessibilitynodeinfosbytexts (this.rootnodeinfo, New String[]{
  "Remove red Envelopes"});
   if (!nodes2.isempty ()) {L.D ("Node2!= null"); for (Accessibilitynodeinfo nodeinfo:nodes2) {if (NO)Deinfo.getclassname (). Equals ("Android.widget.Button")) Nodeinfo.performaction (accessibilitynodeinfo.action_
   CLICK);  } else {* * stamp a red envelope, the red envelope has not yet been robbed, traversal node matching "Split red envelope" * * accessibilitynodeinfo Node2 = (This.rootNodeInfo.getChildCount () > 3) ?
   This.rootNodeInfo.getChild (3): null;
    if (node2!= null && node2.getclassname (). Equals ("Android.widget.Button")) {munpacknode = Node2;
    Mneedunpack = true;
    Istogetmoney = true;
    L.D ("Find Red packet!");
   Return }/* Stamp red envelopes, red envelopes have been robbed, traversal node matching "has been deposited in small change" and "Hange" * * (mluckymoneypicked) {list<accessibilitynodeinfo> nodes3 = th Is.findaccessibilitynodeinfosbytexts (This.rootnodeinfo, New string[]{red_packet_picked, RED_PACKET_SAVE, RED_
   Packet_picked2, red_packet_picked_detail});    
    if (!nodes3.isempty ()) {L.D ("!nodes3.isempty ()");
    if (Rootnodeinfo.getchildcount () > 1) {l.d ("red_packet_picked!"); else {L.D ("nodes3.get (0). ToString ():" + nodes3.get (0). GetText (). toString()); if (!nodes3.get (0). GetText (). toString (). Equals (Red_packet_picked_detail)) {Accessibilitynodeinfo TargetNode = nodes
      3.get (Nodes3.size ()-1);
      Hongbaoinfo.getinfo (TargetNode);
       if (Istogetmoney) {Isgetmoney = true;
       Istogetmoney = false;
       Gotmoney = Hongbaoinfo.getmoney ();
      L.D ("Gotmoney:" + Gotmoney);
      } L.D ("red_packet_save!");
     L.D ("Hongbaoinfo:" + hongbaoinfo.tostring ());
     else {L.D ("This packet is myself!");
    } Mneedback = true;
   mluckymoneypicked = false; }
  }
 }

Mainly through the detection of "collect red envelopes" and other key text information to determine whether there are new red envelopes

Detect the receipt of red envelopes to determine whether " android.widget.LinearLayout ", shielding chat information in the text interference

When taking out a red envelope, because the micro-letter version may be different, at the same time make two judgments to compatible partial version

The need to automatically return the red envelopes, there are several situations: Rob, Hange, and the red envelope is their own red envelopes issued

Here's the code to listen to the chat list:

Private Boolean watchlist (Accessibilityevent event) {
  //not a message
  if (Event.geteventtype ()!= accessibilityevent.type_window_content_changed | | Event.getsource () = null) return
   false;

  list<accessibilitynodeinfo> nodes = Event.getsource (). Findaccessibilitynodeinfosbytext (RED_PACKET_ NOTIFICATION);
  if (!nodes.isempty ()) {
   Accessibilitynodeinfo Nodetoclick = nodes.get (0);
   Charsequence contentdescription = Nodetoclick.getcontentdescription ();
   if (contentdescription!= null &&!lastcontentdescription.equals (contentdescription)) {
    Nodetoclick.performaction (Accessibilitynodeinfo.action_click);
    Lastcontentdescription = Contentdescription.tostring ();
    return true;
   }
  return false;
 }

Here is the code to listen for notification information:

 Private Boolean watchnotifications (Accessibilityevent event) {
  //not a notification
  if (Event.geteventtype ()! = accessibilityevent.type_notification_state_changed) return
   false;

  Not a hongbao
  String tip = Event.gettext (). toString ();
  if (!tip.contains (red_packet_notification)) return true;

  Parcelable parcelable = Event.getparcelabledata ();
  if (parcelable instanceof Notification) {
   Notification Notification = (Notification) parcelable;
   try {
    notification.contentIntent.send ();
   } catch (Pendingintent.canceledexception e) {
    E.printstacktrace ();
   }
  return true;
 }

The acquisition of Red envelope information and the storage of the log

By obtaining the sub information of the node, the sender and the amount of the red envelope, and the time of stealing the red envelopes are respectively obtained, and a simple form is recorded to record the information separately.

@Table (name = "Hongbaoinfos") public class Hongbaoinfo extends Model {private int month;
 private int day;
 private int hour;
 private int min;

 private int sec;

 @Column (name = "Sender") public String sender;

 @Column (name = ' money ') public String;

 @Column (name = ' time ') public String time;
  public void GetInfo (Accessibilitynodeinfo node) {Accessibilitynodeinfo Hongbaonode = node.getparent ();
  Sender = Hongbaonode.getchild (0). GetText (). toString ();
  Money = Hongbaonode.getchild (2). GetText (). toString ();
 Time = Getstringtime ();
  Private String Getstringtime () {Calendar c = calendar.getinstance ();
  month = C.get (calendar.month) + 1;
  Day = C.get (calendar.day_of_month);
  hour = C.get (Calendar.hour_of_day);
  min = C.get (calendar.minute);
  SEC = C.get (Calendar.second);
 return month+ "month" +day+ "Day" +hour+ ":" +min+ ":" +SEC;  @Override public String toString () {return "Hongbaoinfo [sender=] + sender +", money= "+ Money +", time= "+ Time"
 + "]"; } PUBlic static list 

Storage operations:

 private void Savetolog (Hongbaoinfo hongbaoinfo) {
  if (watchedflags.get ("Pref_etc_log")) {
   Hongbaoinfo HongbaoInfo1 = new Hongbaoinfo ();
   HongbaoInfo1 = Hongbaoinfo;
   Hongbaoinfo1.save ();
  } else {
   L.D ("Log closed!");
  }
 

Summarize

The main code is basically finished here, currently in the micro-letter the latest version of the test OK, there are still the following problems:

1. The same person can not automatically rob a continuous, because in order to prevent repeated clicks do the filter, the same person's red envelopes will not click again after the

2. AccessibilityService after a long opening time will sometimes be switched off by the system

Conclusion

The above is the entire content of this article, I hope for everyone's study and work can 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.