Android 輔助功能 -搶紅包(二)
本篇文章繼續(xù)講述輔助功能實現搶紅包的方案.
上篇文章主要講了下輔助功能的基本使用,本文涉及的一些基礎內容就不再贅述了.
有疑問的可以查看上篇文章:
1: 添加微信監(jiān)聽
修改xml文件,android:packageNames中新增微信監(jiān)聽.
<?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: 監(jiān)聽通知欄消息
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("紅包:恭喜發(fā)財");
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 ->夏沫: [微信紅包] 恭喜發(fā)財,大吉大利
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
如上述日志輸出,監(jiān)聽通知欄消息后,延遲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
如上述輸出,監(jiān)聽通知欄添加好友消息,延遲1s跳轉到了新的朋友界面.
本文由博客一文多發(fā)平臺 OpenWrite 發(fā)布焚虱!