实现原理
通过利用 AccessibilityService 辅助服务,监测屏幕内容,如监听状态栏的信息,屏幕跳转等,以此来实现自动拆红包的功能。关于 AccessibilityService 辅助服务,可以自行百度了解更多。
代码基础:
1. 首先声明一个 RedPacketService 继承自 AccessibilityService,该服务类有两个方法必须重写,如下:
- /**
- * Created by Yemon on 2017/2/3.
- * email:879509347@qq.com
- *
- * 抢红包服务类
- */
- public class RedPacketService extends AccessibilityService {
- /**
- * 必须重写的方法:此方法用了接受系统发来的event。在你注册的event发生是被调用。在整个生命周期会被调用多次。
- */
- @Override
- public void onAccessibilityEvent(AccessibilityEvent event) {
- }
- /**
- * 必须重写的方法:系统要中断此service返回的响应时会调用。在整个生命周期会被调用多次。
- */
- @Override
- public void onInterrupt() {
- Toast.makeText(this, "我快被终结了啊-----", Toast.LENGTH_SHORT).show();
- }
- /**
- * 服务已连接
- */
- @Override
- protected void onServiceConnected() {
- Toast.makeText(this, "抢红包服务开启", Toast.LENGTH_SHORT).show();
- super.onServiceConnected();
- }
- /**
- * 服务已断开
- */
- @Override
- public boolean onUnbind(Intent intent) {
- Toast.makeText(this, "抢红包服务已被关闭", Toast.LENGTH_SHORT).show();
- return super.onUnbind(intent);
- }
- }
2. 对我们的 RedPacketService 进行一些配置,这里配置方法可以选择代码动态配置(onServiceConnected 里配置),也可以直接在 res/xml 下新建. xml 文件,没有 xml 文件夹就新建。这里我们将文件命名为 redpacket_service_config.xml,代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
- android:accessibilityEventTypes="typeAllMask"
- android:accessibilityFeedbackType="feedbackGeneric"
- android:accessibilityFlags="flagDefault"
- android:canRetrieveWindowContent="true"
- android:description="@string/desc"
- android:notificationTimeout="100"
- android:packageNames="com.tencent.mm" />
accessibilityEventTypes:
响应哪一种类型的事件,typeAllMask 就是响应所有类型的事件了,另外还有单击、长按、滑动等。
accessibilityFeedbackType:
用什么方式反馈给用户,有语音播出和振动。可以配置一些 TTS 引擎,让它实现发音。
packageNames:
指定响应哪个应用的事件。这里我们是写抢红包助手,就写微信的包名:com.tencent.mm,这样就可以监听微信产生的事件了。
notificationTimeout:
响应时间
description:
辅助服务的描述信息。
3.service 是四大组件之一,需要在 AndroidManifest 进行配置,注意这里稍微有些不同:
- <!--抢红包服务-->
- <service
- android:name=".RedPacketService"
- android:enabled="true"
- android:exported="true"
- android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
- <intent-filter>
- <action android:name="android.accessibilityservice.AccessibilityService" />
- </intent-filter>
- <meta-data
- android:name="android.accessibilityservice"
- android:resource="@xml/redpacket_service_config"></meta-data>
- </service>
- android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" 权限申请
- android:resource="@xml/redpacket_service_config" 引用刚才的配置文件
- 核心代码:
- 我们的红包助手,核心思路分为三步走:
- 监听通知栏微信消息,如果弹出[微信红包]字样,模拟手指点击状态栏跳转到微信聊天界面→在微信聊天界面查找红包,如果找到则模拟手指点击打开,弹出打开红包界面→模拟手指点击红包"開"
- 1.监听通知栏消息,查看是否有[微信红包]字样,代码如下:
- @Override
- public void onAccessibilityEvent(AccessibilityEvent event) {
- int eventType = event.getEventType();
- switch (eventType) {
- //通知栏来信息,判断是否含有微信红包字样,是的话跳转
- case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
- List<CharSequence> texts = event.getText();
- for (CharSequence text : texts) {
- String content = text.toString();
- if (!TextUtils.isEmpty(content)) {
- //判断是否含有[微信红包]字样
- if (content.contains("[微信红包]")) {
- //如果有则打开微信红包页面
- openWeChatPage(event);
- }
- }
- }
- break;
- }
- }
- /**
- * 开启红包所在的聊天页面
- */
- private void openWeChatPage(AccessibilityEvent event) {
- //A instanceof B 用来判断内存中实际对象A是不是B类型,常用于强制转换前的判断
- if (event.getParcelableData() != null && event.getParcelableData() instanceof Notification) {
- Notification notification = (Notification) event.getParcelableData();
- //打开对应的聊天界面
- PendingIntent pendingIntent = notification.contentIntent;
- try {
- pendingIntent.send();
- } catch (PendingIntent.CanceledException e) {
- e.printStackTrace();
- }
- }
- }
- 2.判断当前是否在微信聊天页面,是的话遍历当前页面各个控件,找到含有微信红包或者领取红包的textview控件,然后逐层找到他的可点击父布局(图中绿色部分),模拟点击跳转到含有"開"的红包界面,
- 代码如下:
- @Override
- public void onAccessibilityEvent(AccessibilityEvent event) {
- int eventType = event.getEventType();
- switch (eventType) {
- //窗口发生改变时会调用该事件
- case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
- String className = event.getClassName().toString();
- //判断是否是微信聊天界面
- if ("com.tencent.mm.ui.LauncherUI".equals(className)) {
- //获取当前聊天页面的根布局
- AccessibilityNodeInfo rootNode = getRootInActiveWindow();
- //开始找红包
- findRedPacket(rootNode);
- }
- }
- }
- /**
- * 遍历查找红包
- */
- private void findRedPacket(AccessibilityNodeInfo rootNode) {
- if (rootNode != null) {
- //从最后一行开始找起
- for (int i = rootNode.getChildCount() - 1; i >= 0; i--) {
- AccessibilityNodeInfo node = rootNode.getChild(i);
- //如果node为空则跳过该节点
- if (node == null) {
- continue;
- }
- CharSequence text = node.getText();
- if (text != null && text.toString().equals("领取红包")) {
- AccessibilityNodeInfo parent = node.getParent();
- //while循环,遍历"领取红包"的各个父布局,直至找到可点击的为止
- while (parent != null) {
- if (parent.isClickable()) {
- //模拟点击
- parent.performAction(AccessibilityNodeInfo.ACTION_CLICK);
- //isOpenRP用于判断该红包是否点击过
- isOpenRP = true;
- break;
- }
- parent = parent.getParent();
- }
- }
- //判断是否已经打开过那个最新的红包了,是的话就跳出for循环,不是的话继续遍历
- if (isOpenRP) {
- break;
- } else {
- findRedPacket(node);
- }
- }
- }
- }
3. 点击红包后,在模拟手指点击 "開" 以此开启红包,跳转到红包详情界面,方法与步骤二类似:
- @Override
- public void onAccessibilityEvent(AccessibilityEvent event) {
- int eventType = event.getEventType();
- switch (eventType) {
- //窗口发生改变时会调用该事件
- case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
- String className = event.getClassName().toString();
- //判断是否是显示'开'的那个红包界面
- if ("com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyReceiveUI".equals(className)) {
- AccessibilityNodeInfo rootNode = getRootInActiveWindow();
- //开始抢红包
- openRedPacket(rootNode);
- }
- break;
- }
- }
- /**
- * 开始打开红包
- */
- private void openRedPacket(AccessibilityNodeInfo rootNode) {
- for (int i = 0; i < rootNode.getChildCount(); i++) {
- AccessibilityNodeInfo node = rootNode.getChild(i);
- if ("android.widget.Button".equals(node.getClassName())) {
- node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
- }
- openRedPacket(node);
- }
- }
结合以上三步,下面是完整代码,注释已经写的很清楚,直接看代码:
- package com.cxk.redpacket;
- import android.accessibilityservice.AccessibilityService;
- import android.app.KeyguardManager;
- import android.app.Notification;
- import android.app.PendingIntent;
- import android.app.Service;
- import android.content.Context;
- import android.content.Intent;
- import android.os.IBinder;
- import android.os.PowerManager;
- import android.text.TextUtils;
- import android.util.Log;
- import android.view.accessibility.AccessibilityEvent;
- import android.view.accessibility.AccessibilityNodeInfo;
- import android.widget.Toast;
- import java.util.List;
- /**
- * 抢红包Service,继承AccessibilityService
- */
- public class RedPacketService extends AccessibilityService {
- /**
- * 微信几个页面的包名+地址。用于判断在哪个页面 LAUCHER-微信聊天界面,LUCKEY_MONEY_RECEIVER-点击红包弹出的界面
- */
- private String LAUCHER = "com.tencent.mm.ui.LauncherUI";
- private String LUCKEY_MONEY_DETAIL = "com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyDetailUI";
- private String LUCKEY_MONEY_RECEIVER = "com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyReceiveUI";
- /**
- * 用于判断是否点击过红包了
- */
- private boolean isOpenRP;
- @Override
- public void onAccessibilityEvent(AccessibilityEvent event) {
- int eventType = event.getEventType();
- switch (eventType) {
- //通知栏来信息,判断是否含有微信红包字样,是的话跳转
- case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
- List<CharSequence> texts = event.getText();
- for (CharSequence text : texts) {
- String content = text.toString();
- if (!TextUtils.isEmpty(content)) {
- //判断是否含有[微信红包]字样
- if (content.contains("[微信红包]")) {
- //如果有则打开微信红包页面
- openWeChatPage(event);
- isOpenRP=false;
- }
- }
- }
- break;
- //界面跳转的监听
- case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
- String className = event.getClassName().toString();
- //判断是否是微信聊天界面
- if (LAUCHER.equals(className)) {
- //获取当前聊天页面的根布局
- AccessibilityNodeInfo rootNode = getRootInActiveWindow();
- //开始找红包
- findRedPacket(rootNode);
- }
- //判断是否是显示'开'的那个红包界面
- if (LUCKEY_MONEY_RECEIVER.equals(className)) {
- AccessibilityNodeInfo rootNode = getRootInActiveWindow();
- //开始抢红包
- openRedPacket(rootNode);
- }
- //判断是否是红包领取后的详情界面
- if(LUCKEY_MONEY_DETAIL.equals(className)){
- //返回桌面
- back2Home();
- }
- break;
- }
- }
- /**
- * 开始打开红包
- */
- private void openRedPacket(AccessibilityNodeInfo rootNode) {
- for (int i = 0; i < rootNode.getChildCount(); i++) {
- AccessibilityNodeInfo node = rootNode.getChild(i);
- if ("android.widget.Button".equals(node.getClassName())) {
- node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
- }
- openRedPacket(node);
- }
- }
- /**
- * 遍历查找红包
- */
- private void findRedPacket(AccessibilityNodeInfo rootNode) {
- if (rootNode != null) {
- //从最后一行开始找起
- for (int i = rootNode.getChildCount() - 1; i >= 0; i--) {
- AccessibilityNodeInfo node = rootNode.getChild(i);
- //如果node为空则跳过该节点
- if (node == null) {
- continue;
- }
- CharSequence text = node.getText();
- if (text != null && text.toString().equals("领取红包")) {
- AccessibilityNodeInfo parent = node.getParent();
- //while循环,遍历"领取红包"的各个父布局,直至找到可点击的为止
- while (parent != null) {
- if (parent.isClickable()) {
- //模拟点击
- parent.performAction(AccessibilityNodeInfo.ACTION_CLICK);
- //isOpenRP用于判断该红包是否点击过
- isOpenRP = true;
- break;
- }
- parent = parent.getParent();
- }
- }
- //判断是否已经打开过那个最新的红包了,是的话就跳出for循环,不是的话继续遍历
- if (isOpenRP) {
- break;
- } else {
- findRedPacket(node);
- }
- }
- }
- }
- /**
- * 开启红包所在的聊天页面
- */
- private void openWeChatPage(AccessibilityEvent event) {
- //A instanceof B 用来判断内存中实际对象A是不是B类型,常用于强制转换前的判断
- if (event.getParcelableData() != null && event.getParcelableData() instanceof Notification) {
- Notification notification = (Notification) event.getParcelableData();
- //打开对应的聊天界面
- PendingIntent pendingIntent = notification.contentIntent;
- try {
- pendingIntent.send();
- } catch (PendingIntent.CanceledException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 服务连接
- */
- @Override
- protected void onServiceConnected() {
- Toast.makeText(this, "抢红包服务开启", Toast.LENGTH_SHORT).show();
- super.onServiceConnected();
- }
- /**
- * 必须重写的方法:系统要中断此service返回的响应时会调用。在整个生命周期会被调用多次。
- */
- @Override
- public void onInterrupt() {
- Toast.makeText(this, "我快被终结了啊-----", Toast.LENGTH_SHORT).show();
- }
- /**
- * 服务断开
- */
- @Override
- public boolean onUnbind(Intent intent) {
- Toast.makeText(this, "抢红包服务已被关闭", Toast.LENGTH_SHORT).show();
- return super.onUnbind(intent);
- }
- /**
- * 返回桌面
- */
- private void back2Home() {
- Intent home=new Intent(Intent.ACTION_MAIN);
- home.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- home.addCategory(Intent.CATEGORY_HOME);
- startActivity(home);
- }
- }
使用方法:
设置 - 辅助功能 - 无障碍 - 点击 RedPacket 开启即可
已知问题:
1. 聊天列表或者聊天界面中无法直接自动抢红包
2. 未做熄屏自动抢红包处理,想要熄屏能自动抢红包的同学直接把开屏代码写在第一步即可。
来源: