Android 輔助功能 -搶紅包(二) 本篇文章繼續講述輔助功能實現搶紅包的方案. 上篇文章主要講了下輔助功能的基本使用,本文涉及的一些基礎內容就不再贅述了. 有疑問的可以查看上篇文章: Android 輔助功能 -搶紅包 1: 添加微信監聽 修改xml文件,android:packageName ...
Android 輔助功能 -搶紅包(二)
本篇文章繼續講述輔助功能實現搶紅包的方案.
上篇文章主要講了下輔助功能的基本使用,本文涉及的一些基礎內容就不再贅述了.
有疑問的可以查看上篇文章:
1: 添加微信監聽
修改xml文件,android:packageNames中新增微信監聽.
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFeedbackType="feedbackAllMask"
android:accessibilityFlags="flagDefault"
android:canRetrieveWindowContent="true"
android:canRequestFilterKeyEvents="true"
android:description="@string/desc"
android:packageNames= "com.test.redpacket,com.tencent.mm">
</accessibility-service>
2: 監聽通知欄消息
public class RedPacketAccessibilityService extends AccessibilityService {
private static final String TAG = "RedPacketAccessibilityService";
@SuppressLint("LongLogTag")
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
int action = event.getAction();
Log.i(TAG, "onAccessibilityEvent: " + action);
CharSequence packageName = event.getPackageName();
Log.i(TAG, "onAccessibilityEvent: " + packageName);
AccessibilityNodeInfo rootInActiveWindow = getRootInActiveWindow();
if (rootInActiveWindow != null) {
List<AccessibilityNodeInfo> nodeInfos = rootInActiveWindow.findAccessibilityNodeInfosByText("紅包:恭喜發財");
if (nodeInfos != null) {
for (AccessibilityNodeInfo nodeInfo : nodeInfos) {
nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
}
}
}
switch (event.getEventType()) {
case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
List<CharSequence> text = event.getText();
for (CharSequence charSequence : text) {
Log.i(TAG, "onAccessibilityEvent: notification ->" + charSequence);
if (charSequence.toString().contains("請求添加")) {
Parcelable parcelableData = event.getParcelableData();
if (parcelableData != null && parcelableData instanceof Notification) {
PendingIntent contentIntent = ((Notification) parcelableData).contentIntent;
new Handler().postDelayed(() -> {
try {
contentIntent.send();
} catch (PendingIntent.CanceledException e) {
Log.e(TAG, "onAccessibilityEvent: ", e);
}
},1000);
}
}
}
break;
}
}
@Override
public void onInterrupt() {
}
}
3: 測試
2024-03-15 12:58:49.361 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: com.tencent.mm
2024-03-15 12:58:49.386 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: notification ->夏沫: [微信紅包] 恭喜發財,大吉大利
2024-03-15 12:58:50.716 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: 0
2024-03-15 12:58:50.716 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: com.tencent.mm
2024-03-15 12:58:50.874 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: 0
2024-03-15 12:58:50.874 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: com.tencent.mm
2024-03-15 12:58:50.903 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: 0
如上述日誌輸出,監聽通知欄消息後,延遲1s 跳轉到了聊天界面.
2024-03-15 12:59:04.633 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: 0
2024-03-15 12:59:04.633 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: com.tencent.mm
2024-03-15 12:59:04.640 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: notification ->夏沫請求添加你為朋友
2024-03-15 12:59:05.809 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: 0
2024-03-15 12:59:05.809 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: com.tencent.mm
2024-03-15 12:59:05.831 10332-10332/com.test.redpacket I/RedPacketAccessibilityService: onAccessibilityEvent: 0
如上述輸出,監聽通知欄添加好友消息,延遲1s跳轉到了新的朋友界面.
本文由博客一文多發平臺 OpenWrite 發佈!