博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android中微信抢红包助手的实现
阅读量:4627 次
发布时间:2019-06-09

本文共 14520 字,大约阅读时间需要 48 分钟。

参考(感谢作者):http://www.jianshu.com/p/cd1cd53909d7

http://blog.csdn.net/jiangwei0910410003/article/details/48895153

 

实现原理

  通过利用AccessibilityService辅助服务,监测屏幕内容,如监听状态栏的信息,屏幕跳转等,以此来实现自动拆红包的功能。关于AccessibilityService辅助服务,可以自行百度了解更多。

 

代码基础:

1.首先声明一个RedPacketService继承自AccessibilityService,该服务类有两个方法必须重写,如下:

/** * Created by cxk on 2017/2/3. * email:471497226@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,代码如下:

accessibilityEventTypes:   

响应哪一种类型的事件,typeAllMask就是响应所有类型的事件了,另外还有单击、长按、滑动等。

accessibilityFeedbackType:  

用什么方式反馈给用户,有语音播出和振动。可以配置一些TTS引擎,让它实现发音。

packageNames:

指定响应哪个应用的事件。这里我们是写抢红包助手,就写微信的包名:com.tencent.mm,这样就可以监听微信产生的事件了。

notificationTimeout:

响应时间

description:

辅助服务的描述信息。

 

3.service是四大组件之一,需要在AndroidManifest进行配置,注意这里稍微有些不同:

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
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.Instrumentation;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.KeyEvent;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-点击红包弹出的界面     * LUCKEY_MONEY_DETAIL-红包领取后的详情界面     */    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;    private boolean isOpenDetail = false;    /**     * 用于判断是否屏幕是亮着的     */    private boolean isScreenOn;    /**     * 获取PowerManager.WakeLock对象     */    private PowerManager.WakeLock wakeLock;    /**     * KeyguardManager.KeyguardLock对象     */    private KeyguardManager.KeyguardLock keyguardLock;    @Override    public void onAccessibilityEvent(AccessibilityEvent event) {        int eventType = event.getEventType();        switch (eventType) {            //通知栏来信息,判断是否含有微信红包字样,是的话跳转            case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:                List
texts = event.getText(); for (CharSequence text : texts) { String content = text.toString(); if (!TextUtils.isEmpty(content)) { //判断是否含有[微信红包]字样 if (content.contains("[微信红包]")) { if (!isScreenOn()) { wakeUpScreen(); } //如果有则打开微信红包页面 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 (isOpenDetail && LUCKEY_MONEY_DETAIL.equals(className)) { isOpenDetail = false; //返回桌面 back2Home(); //如果之前是锁着屏幕的则重新锁回去 release(); } 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); isOpenDetail = true; } 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); } /** * 判断是否处于亮屏状态 * * @return true-亮屏,false-暗屏 */ private boolean isScreenOn() { PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); isScreenOn = pm.isScreenOn(); Log.e("isScreenOn", isScreenOn + ""); return isScreenOn; } /** * 解锁屏幕 */ private void wakeUpScreen() { //获取电源管理器对象 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); //后面的参数|表示同时传入两个值,最后的是调试用的Tag wakeLock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK, "bright"); //点亮屏幕 wakeLock.acquire(); //得到键盘锁管理器 KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); keyguardLock = km.newKeyguardLock("unlock"); //解锁 keyguardLock.disableKeyguard(); } /** * 释放keyguardLock和wakeLock */ public void release() { if (keyguardLock != null) { keyguardLock.reenableKeyguard(); keyguardLock = null; } if (wakeLock != null) { wakeLock.release(); wakeLock = null; } }}

使用方法:

设置-辅助功能-无障碍-点击RedPacket开启即可(或者直接在设置搜索辅助功能or RedPacket)

注:因为AccessibilityService服务很容易断开,所以我们需要将我们的App设置为白名单,防止被系统KO掉。这样他就能一直跑在我们的后台啦。

 

已知问题:

1.聊天列表或者聊天界面中无法直接自动抢红包

Demo下载地址:https://github.com/CKTim/RedPacket

 

 

转载于:https://www.cnblogs.com/cxk1995/p/6363574.html

你可能感兴趣的文章
表达式引擎aviator
查看>>
Git,Github和Gitlab简介和使用方法
查看>>
每天一个linux命令(30): chown命令
查看>>
Python 的property的实现 .
查看>>
搭建Hadoop集群步骤
查看>>
提升jmeter自身性能
查看>>
wordpress调用树形目录
查看>>
数据结构与算法分析(C++版)(第二版)
查看>>
tomcat启动
查看>>
在python3环境安装builtwith模块
查看>>
c语言:婚礼上的谎言
查看>>
[bzoj2333] [SCOI2011]棘手的操作 (可并堆)
查看>>
MVC 4.0 Razor模板引擎 @Html.RenderPartial 与 @Html.RenderAction 区别
查看>>
DHCP服务搭建
查看>>
在路上●我的年轻●勇往直前●匆匆十年
查看>>
LOJ 2721 「NOI2018」屠龙勇士——扩展中国剩余定理
查看>>
hdu3321
查看>>
poj1741(树的点分治)
查看>>
word 生成HTML
查看>>
JSONP--解决ajax跨域问题
查看>>