Android辅助功能AccessibilityService与抢红包辅助
程序员文章站
2024-02-23 13:46:04
推荐阅读:android中微信抢红包插件原理解析及开发思路
抢红包的原理都差不多,一般是用android的辅助功能(accessibilityservice类)先监听通知...
抢红包的原理都差不多,一般是用android的辅助功能(accessibilityservice类)先监听通知栏事件或窗口变化事件来查找红包关键字然后去模拟点击或打开红包。
下面附上源码,程序已实现自动抢红包,锁屏黑屏状态自动解锁亮屏,android4.x测试通过。函数具体功能请看详细注释。
注:在聊天界面收到红包不会自动打开,因为通知栏没有消息提示从而监听不了,此时只需手动点一下即可。其他未知情况请自行用logcat调试,源码已经有相关的调试信息。软件仅供学习娱乐。
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style="font-family: arial, helvetica, sans-serif; color: rgb(192, 192, 192);"></span><pre style="margin-top: 0px; margin-bottom: 0px;">import java.util.calendar; import java.util.list; import android.accessibilityservice.accessibilityservice; import android.annotation.suppresslint; import android.app.keyguardmanager; import android.app.keyguardmanager.keyguardlock; import android.app.notification; import android.app.pendingintent; import android.app.pendingintent.canceledexception; import android.content.context; import android.media.mediaplayer; import android.os.powermanager; import android.util.log; import android.view.accessibility.accessibilityevent; import android.view.accessibility.accessibilitynodeinfo; import android.widget.toast; public class demo extends accessibilityservice { private boolean canget = false;//能否点击红包 private boolean enablekeyguard = true;//默认有屏幕锁 //窗口状态 private static final int window_none = 0; private static final int window_luckymoney_receiveui = 1; private static final int window_luckymoney_detail = 2; private static final int window_launcher = 3; private static final int window_other = -1; //当前窗口 private int mcurrentwindow = window_none; //锁屏、解锁相关 private keyguardmanager km; private keyguardlock kl; //唤醒屏幕相关 private powermanager pm; private powermanager.wakelock wl = null; //播放提示声音 private mediaplayer player; public void playsound(context context) { calendar cal = calendar.getinstance(); int hour = cal.get(calendar.hour_of_day); //夜间不播放提示音 if(hour > 7 && hour < 22) { player.start(); } } //唤醒屏幕和解锁 private void wakeandunlock(boolean unlock) { if(unlock) { //若为黑屏状态则唤醒屏幕 if(!pm.isscreenon()) { //获取电源管理器对象,acquire_causes_wakeup这个参数能从黑屏唤醒屏幕 wl = pm.newwakelock(powermanager.screen_bright_wake_lock | powermanager.acquire_causes_wakeup, "bright"); //点亮屏幕 wl.acquire(); log.i("demo", "亮屏"); } //若在锁屏界面则解锁直接跳过锁屏 if(km.inkeyguardrestrictedinputmode()) { //设置解锁标志,以判断抢完红包能否锁屏 enablekeyguard = false; //解锁 kl.disablekeyguard(); log.i("demo", "解锁"); } } else { //如果之前解过锁则加锁以恢复原样 if(!enablekeyguard) { //锁屏 kl.reenablekeyguard(); log.i("demo", "加锁"); } //若之前唤醒过屏幕则释放之使屏幕不保持常亮 if(wl != null) { wl.release(); wl = null; log.i("demo", "关灯"); } } } //通过文本查找节点 public accessibilitynodeinfo findnodeinfosbytext(accessibilitynodeinfo nodeinfo, string text) { list<accessibilitynodeinfo> list = nodeinfo.findaccessibilitynodeinfosbytext(text); if(list == null || list.isempty()) { return null; } return list.get(0); } //模拟点击事件 public void performclick(accessibilitynodeinfo nodeinfo) { if(nodeinfo == null) { return; } if(nodeinfo.isclickable()) { nodeinfo.performaction(accessibilitynodeinfo.action_click); } else { performclick(nodeinfo.getparent()); } } //模拟返回事件 public void performback(accessibilityservice service) { if(service == null) { return; } service.performglobalaction(accessibilityservice.global_action_back); } //实现辅助功能 @override public void onaccessibilityevent(accessibilityevent event) { int eventtype = event.geteventtype(); log.i("demo", integer.tostring(eventtype)); switch (eventtype) { //第一步:监听通知栏消息 case accessibilityevent.type_notification_state_changed: list<charsequence> texts = event.gettext(); if (!texts.isempty()) { for (charsequence text : texts) { string content = text.tostring(); log.i("demo", "text:"+content); //收到红包提醒 if (content.contains("[微信红包]")||content.contains("[qq红包]")) { //模拟打开通知栏消息 if (event.getparcelabledata() != null && event.getparcelabledata() instanceof notification) { //播放提示音 playsound(this); //若是微信红包则解锁并自动打开,若是qq红包则只提示并跳转到有红包的聊天界面,暂未实现qq红包自动领取功能 if(content.contains("[微信红包]")) wakeandunlock(true); log.i("demo", "canget=true"); canget = true; try { notification notification = (notification) event.getparcelabledata(); pendingintent pendingintent = notification.contentintent; pendingintent.send(); } catch (canceledexception e) { e.printstacktrace(); } } break; } } } break; //第二步:监听是否进入微信红包消息界面 case accessibilityevent.type_window_state_changed: string classname = event.getclassname().tostring(); if (classname.equals("com.tencent.mm.ui.launcherui")) { mcurrentwindow = window_launcher; //开始抢红包 log.i("demo", "准备抢红包..."); getpacket(); } else if (classname.equals("com.tencent.mm.plugin.luckymoney.ui.luckymoneyreceiveui")) { mcurrentwindow = window_luckymoney_receiveui; //开始打开红包 log.i("demo", "打开红包"); openpacket(); wakeandunlock(false); } else if(classname.equals("com.tencent.mm.plugin.luckymoney.ui.luckymoneydetailui")) { mcurrentwindow = window_luckymoney_detail; //返回以方便下次收红包 log.i("demo", "返回"); performback(this); } else { mcurrentwindow = window_other; } break; case accessibilityevent.type_window_content_changed: if(mcurrentwindow != window_launcher) { //不在聊天界面或聊天列表,不处理 return; } if(canget) { getpacket(); } break; } } //找到红包并点击 @suppresslint("newapi") private void getpacket() { accessibilitynodeinfo nodeinfo = getrootinactivewindow(); if (nodeinfo == null) { return; } // 找到领取红包的点击事件 list<accessibilitynodeinfo> list = nodeinfo.findaccessibilitynodeinfosbytext("领取红包"); if(list != null ) { if(list.isempty()) { log.i("demp", "领取列表为空"); // 从消息列表查找红包 accessibilitynodeinfo node = findnodeinfosbytext(nodeinfo, "[微信红包]"); if(node != null) { canget = true; performclick(node); } } else { if(canget) { //最新的红包领起 accessibilitynodeinfo node = list.get(list.size() - 1); performclick(node); log.i("demo", "canget=false"); canget = false; } } } } //打开红包 @suppresslint("newapi") private void openpacket() { accessibilitynodeinfo nodeinfo = getrootinactivewindow(); if(nodeinfo == null) { return; } log.i("demo", "查找打开按钮..."); accessibilitynodeinfo targetnode = null; //如果红包已经被抢完则直接返回 targetnode = findnodeinfosbytext(nodeinfo, "看看大家的手气"); if(targetnode != null) { performback(this); return; } //通过组件名查找开红包按钮,还可通过组件id直接查找但需要知道id且id容易随版本更新而变化,旧版微信还可直接搜“開”字找到按钮 if(targetnode == null) { log.i("demo", "打开按钮中..."); for (int i = 0; i < nodeinfo.getchildcount(); i++) { accessibilitynodeinfo node = nodeinfo.getchild(i); if("android.widget.button".equals(node.getclassname())) { targetnode = node; break; } } } //若查找到打开按钮则模拟点击 if(targetnode != null) { final accessibilitynodeinfo n = targetnode; performclick(n); } } @override public void oninterrupt() { toast.maketext(this, "抢红包服务被中断啦~", toast.length_long).show(); } @override protected void onserviceconnected() { super.onserviceconnected(); log.i("demo", "开启"); //获取电源管理器对象 pm=(powermanager)getsystemservice(context.power_service); //得到键盘锁管理器对象 km= (keyguardmanager)getsystemservice(context.keyguard_service); //初始化一个键盘锁管理器对象 kl = km.newkeyguardlock("unlock"); //初始化音频 player = mediaplayer.create(this, r.raw.songtip_m); toast.maketext(this, "_已开启抢红包服务_", toast.length_long).show(); } @override public void ondestroy() { super.ondestroy(); log.i("demo", "关闭"); wakeandunlock(false); toast.maketext(this, "_已关闭抢红包服务_", toast.length_long).show(); } }
androidmanifest.xml中声明相关服务和权限
<uses-permission android:name="android.permission.wake_lock" /> <uses-permission android:name="android.permission.disable_keyguard" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <pre name="code" class="html"><service android:name="com.example.test.demo" android:enabled="true" android:exported="true" android:label="@string/app_name" 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="@layout/accessibility_config"/></service></application>
accessibility_config.xml服务配置内容如下
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" android:accessibilityeventtypes="typenotificationstatechanged|typewindowstatechanged|typewindowcontentchanged" android:accessibilityfeedbacktype="feedbackgeneric" android:accessibilityflags="flagdefault" android:canretrievewindowcontent="true" android:description="@string/desc" android:notificationtimeout="100" android:packagenames= "com.tencent.mm,com.tencent.mobileqq" />
其中description为辅助功能的描述内容,packagenames为监听的程序包名,此处只监听微信和qq的accessibilityeventtypes
以上所述是针对android辅助功能accessibilityservice与抢红包辅助的相关知识,希望对大家有所帮助。
上一篇: Python正则简单实例分析
下一篇: java多线程编程之java线程简介
推荐阅读
-
Android辅助功能AccessibilityService与抢红包辅助
-
Android辅助功能AccessibilityService与抢红包辅助
-
Android辅助功能实现自动抢红包(附源码)
-
Android辅助功能实现自动抢红包(附源码)
-
Android-Accessibility(辅助功能/无障碍,自动安装APP)
-
Android通过辅助功能实现抢微信红包原理简单介绍
-
Android Accessibility(辅助功能)
-
Android-Accessibility(辅助功能/无障碍,自动安装APP)
-
Android Accessibility(辅助功能)
-
Android通过辅助功能实现抢微信红包原理简单介绍