需要 开启该应用的 辅助功能
package krelve.demo.rob;import java.util.List;import android.accessibilityservice.AccessibilityService;import android.annotation.SuppressLint;import android.app.Notification;import android.app.PendingIntent;import android.app.PendingIntent.CanceledException;import android.view.accessibility.AccessibilityEvent;import android.view.accessibility.AccessibilityNodeInfo;public class RobMoney extends AccessibilityService {@Overridepublic void onAccessibilityEvent(AccessibilityEvent event) {int eventType = event.getEventType();switch (eventType) {case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:// 通知栏事件List<CharSequence> texts = event.getText();if (!texts.isEmpty()) {for (CharSequence text : texts) {String content = text.toString();if (content.contains("[微信红包]")) {// 监听到微信红包的notification,打开通知if (event.getParcelableData() != null&& event.getParcelableData() instanceof Notification) {Notification notification = (Notification) event.getParcelableData();PendingIntent pendingIntent = notification.contentIntent;try {pendingIntent.send();} catch (CanceledException e) {e.printStackTrace();}}}}}break;case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:String className = event.getClassName().toString();if (className.equals("com.tencent.mm.ui.LauncherUI")) {getPacket();// 领取红包} else if (className.equals("com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyReceiveUI")) {openPacket();// 打开红包}break;}}@SuppressLint("NewApi")private void openPacket() {AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();if (nodeInfo != null) {List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByText("拆红包");for (AccessibilityNodeInfo n : list) {n.performAction(AccessibilityNodeInfo.ACTION_CLICK);}}}@SuppressLint("NewApi")private void getPacket() {AccessibilityNodeInfo rootNode = getRootInActiveWindow();if (rootNode != null) {List<AccessibilityNodeInfo> nodeInfos = rootNode.findAccessibilityNodeInfosByText("领取红包");for (AccessibilityNodeInfo nodeInfo : nodeInfos) {nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);}}}@Overridepublic void onInterrupt() {}}
评论