QQ搶紅包插件實現
作為一個有女朋友的人,之前跟那些20年的單身狗搶紅包是屢戰屢敗。再加上最近群里面發紅包發的厲害,又想到快要過年了,到時候還不知道群里要發好多紅包,所以我將之前在網上宕的一份微信搶紅包的代碼修改了一下,實現了QQ搶紅包!可以支持搶QQ拼手氣紅包,普通紅包,口令紅包,現在再也不怕20年單身手速的人跟我搶紅包了!
先看測試效果圖:
- 搶QQ口令紅包:
口令紅包.gif
可以看見,只要紅包一發出,自動填寫口令并發出,幫你將紅包搶到手! - 搶QQ拼手氣紅包:
拼手氣紅包.gif
拼手氣紅包也是一樣,只要紅包一發出,自動幫你把紅包搶到手,是不是很爽的感覺? - 搶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年的單身手速了!!!