Android實現QQ搶紅包外掛程式_Android

來源:互聯網
上載者:User

又想到快要過年了,到時候還不知道群裡要發好多紅包,所以我將之前在網上宕的一份微信搶紅包的代碼修改了一下,實現了QQ搶紅包!可以支援搶QQ拼手氣紅包,普通紅包,口令紅包,現在再也不怕20年單身手速的人跟我搶紅包了!
先看測試效果圖:
1.搶QQ口令紅包 

                            

 可以看見,只要紅包一發出,自動填寫口令並發出,幫你將紅包搶到手!
2.搶QQ拼手氣紅包                 

拼手氣紅包也是一樣,只要紅包一發出,自動幫你把紅包搶到手,是不是很爽的感覺?
3.搶QQ好友發送的紅包


只要好友或者群裡的人把紅包一發出,就會第一時間讓你搶到紅包!所以只要在群裡面開啟外掛程式,搶紅包從來都是百發百中!好了廢話不多說了,也不吹噓有多牛多好了,下面直接給大家上代碼:

MainActivity:

/*MainActivity中的代碼基本沒改變:*/public class MainActivity extends AppCompatActivity {  private final Intent mAccessibleIntent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);  private Button switchPlugin;   @Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);      setContentView(R.layout.activity_main);      switchPlugin = (Button) findViewById(R.id.button_accessible);    updateServiceStatus();  }   /*開啟外掛程式的按鈕*/  public void onButtonClicked(View view) {startActivity(mAccessibleIntent);}   @Overrideprotected void onResume() {    super.onResume();    updateServiceStatus();  }}   private void updateServiceStatus() {      boolean serviceEnabled = false;      AccessibilityManager accessibilityManager = (AccessibilityManager) getSystemService(Context.ACCESSIBILITY_SERVICE);    List<AccessibilityServiceInfo> accessibilityServices = accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_GENERIC);      for (AccessibilityServiceInfo info : accessibilityServices) {         if (info.getId().equals(getPackageName() + "/.QQHongbaoService")) {             serviceEnabled = true;             break;           }      }     if (serviceEnabled) {         switchPlugin.setText("關閉外掛程式");        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);     } else {         switchPlugin.setText("開啟外掛程式");         getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);}   }}

這裡是MainActivity中的全部代碼,是不是很少的樣子,主要是實現了一個按鈕去開啟ACCESSIBILITY_SERVICE。這個外掛程式主要就是藉助AccessibilityService這個服務來實現。所以剩下的代碼就都在這個服務中了!
QQHongbaoService:

public class QQHongbaoService extends AccessibilityService { private static final String WECHAT_OPEN_EN = "Open";   private static final String WECHAT_OPENED_EN = "You've opened";   private final static String QQ_DEFAULT_CLICK_OPEN = "點擊拆開";  private final static String QQ_HONG_BAO_PASSWORD = "口令紅包";   private final static String QQ_CLICK_TO_PASTE_PASSWORD = "點擊輸入口令";  private boolean mLuckyMoneyReceived;   private String lastFetchedHongbaoId = null;   private long lastFetchedTime = 0;   private static final int MAX_CACHE_TOLERANCE = 5000;   private AccessibilityNodeInfo rootNodeInfo;   private List<AccessibilityNodeInfo> mReceiveNode;  @TargetApi(Build.VERSION_CODES.KITKAT)   public void recycle(AccessibilityNodeInfo info) {      if (info.getChildCount() == 0) {      /*這個if代碼的作用是:匹配“點擊輸入口令的節點,並點擊這個節點”*/     if(info.getText()!=null&&info.getText().toString().equals(QQ_CLICK_TO_PASTE_PASSWORD)) {    info.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);          }        /*這個if代碼的作用是:匹配文本編輯框後面的發送按鈕,並點擊發送口令*/   if (info.getClassName().toString().equals("android.widget.Button") && info.getText().toString().equals("發送")) {            info.performAction(AccessibilityNodeInfo.ACTION_CLICK);          }      } else {          for (int i = 0; i < info.getChildCount(); i++) {            if (info.getChild(i) != null) {                  recycle(info.getChild(i));             }           }       }    }  @TargetApi(Build.VERSION_CODES.JELLY_BEAN)   @Override   public void onAccessibilityEvent(AccessibilityEvent event) {      this.rootNodeInfo = event.getSource();      if (rootNodeInfo == null) {         return;      }      mReceiveNode = null;     checkNodeInfo();      /* 如果已經接收到紅包並且還沒有戳開 */     if (mLuckyMoneyReceived && (mReceiveNode != null)) {         int size = mReceiveNode.size();         if (size > 0) {             String id = getHongbaoText(mReceiveNode.get(size - 1));              long now = System.currentTimeMillis();             if (this.shouldReturn(id, now - lastFetchedTime))                  return;             lastFetchedHongbaoId = id;              lastFetchedTime = now;              AccessibilityNodeInfo cellNode = mReceiveNode.get(size - 1);               if (cellNode.getText().toString().equals("口令紅包已拆開")) {                 return;              }              cellNode.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);            if (cellNode.getText().toString().equals(QQ_HONG_BAO_PASSWORD)) {         AccessibilityNodeInfo rowNode = getRootInActiveWindow();               if (rowNode == null) {                  Log.e(TAG, "noteInfo is null");                  return;                } else {                   recycle(rowNode);               }             }     mLuckyMoneyReceived = false;           }       }   }  private void checkNodeInfo() {    if (rootNodeInfo == null) {       return;    }    /* 聊天會話視窗,遍曆節點匹配“點擊拆開”,“口令紅包”,“點擊輸入口令” */   List<AccessibilityNodeInfo> nodes1 = this.findAccessibilityNodeInfosByTexts(this.rootNodeInfo, new String[]{QQ_DEFAULT_CLICK_OPEN, QQ_HONG_BAO_PASSWORD, QQ_CLICK_TO_PASTE_PASSWORD, "發送"});    if (!nodes1.isEmpty()) {       String nodeId = Integer.toHexString(System.identityHashCode(this.rootNodeInfo));        if (!nodeId.equals(lastFetchedHongbaoId)) {      mLuckyMoneyReceived = true;         mReceiveNode = nodes1;        }    return;     }  }  private String getHongbaoText(AccessibilityNodeInfo node) {    /* 擷取紅包上的文本 */     String content;    try {         AccessibilityNodeInfo i = node.getParent().getChild(0);         content = i.getText().toString();      } catch (NullPointerException npe) {          return null;         }       return content;  }   private boolean shouldReturn(String id, long duration) {     // ID為空白     if (id == null) return true;     // 名稱和緩衝不一致    if (duration < MAX_CACHE_TOLERANCE && id.equals(lastFetchedHongbaoId)) {        return true;     }    return false;  }   private List<AccessibilityNodeInfo> findAccessibilityNodeInfosByTexts(AccessibilityNodeInfo nodeInfo, String[] texts) {     for (String text : texts) {        if (text == null) continue;         List<AccessibilityNodeInfo> nodes = nodeInfo.findAccessibilityNodeInfosByText(text);        if (!nodes.isEmpty()) {            if (text.equals(WECHAT_OPEN_EN) && !nodeInfo.findAccessibilityNodeInfosByText(WECHAT_OPENED_EN).isEmpty()) {                 continue;            }              return nodes;         }      }      return new ArrayList<>();  }   @Override  public void onInterrupt() {}}

QQHongbaoService的全部代碼也在這裡,代碼不多。首先,在這個服務中主要是通過findAccessibilityNodeInfosByText這個方法去獲我們需要的節點;然後用performAction(AccessibilityNodeInfo.ACTION_CLICK)這個方法去點擊紅包節點,關鍵思路大概就是這樣!另外如果是口令紅包,我們需要先按照上面的步驟將紅包戳開,然後通過performAction(AccessibilityNodeInfo.ACTION_CLICK)去點擊輸入口令,最後再通過點擊去發送即可實現!QQHongbaoService需要在AndroidManifest.xml檔案中註冊,
註冊的<application>節點如下圖:

總體來看,只是將微信搶紅包的代碼做了少量的修改,在這裡要感謝各位大神對微信搶紅包源碼的貢獻!最後也希望這篇文章能給大家有所協助,在搶紅包大戰中虐死單身狗,再也不怕你20年的單身手速了!!!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.