QQ抢红包插件实现,安卓源码,以及详细分析 作为一个有女朋友的人,之前跟那些20年的单身狗抢红包是屡战屡败。再加上最近群里面发红包发的厉害,又想到快要过年了,到时候还不知道群里要发好多红包,所以我将之前在网上宕的一份微信抢红包的代码修改了一下,实现了QQ抢红包!可以支持抢QQ拼手气红包,普通红包,口令红包,现在再也不怕20年单身手速的人跟我抢红包了! 先看测试效果图: 1.抢QQ口令红包 可以看见,只要红包一发出,自动填写口令并发出,帮你将红包抢到手! 2.抢QQ拼手气红包: 拼手气红包也是一样,只要红包一发出,自动帮你把红包抢到手,是不是很爽的感觉? 3.抢QQ好友发送的红包: ~~~ /*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() {} } ~~~ 源码下载地址: QQ抢红包插件实现,安卓源码,以及详细分析 http://bbs.jiandaima.com/thread-130-1-1.html