快过年了 自己在学校也没有事情干 闲着无聊就开始研究一下微信抢红包APP怎么写的 自己也写一个 过年希望有好多红包!!!
抢红包软件需要用到android.accessibilityservice 这个包下面的一些类
其实 Android提供这个包的原意是为那些残值人士 开车不方便或者儿童用的 给他们方便 不用动手操作 可是却被人 用来抢红包!!!
首先分析一下几个类:
AccessibilityServiceInfo
这个类是配置类 用来配置Service启动的一些信息 不过Android4.0后提供了xml配置方式 所以基本用不到这个类不过AccessibilityService一些配置项我们还是要了解一下
android:accessibilityEventTypes 监听的类型 比如说typeNotificationStateChanged 就是通知栏的变化 一旦有变化就会发送通知
android:accessibilityFeedbackType 返回的类型 这个还不是很懂
android:canRequestFilterKeyEvents 是否能筛选关键事件
android:canRetrieveWindowContent 是否可以捕捉桌面的改变
android:description 描述性文字
android:notificationTimeout 两个事件之间的间隔
android:packageNames 监听那个包名 即时哪个应用
所以这个类主要是用于配置用的
AccessibilityService
这个类是一个服务 需要 在辅助服务中将这个服务开启 它才会工作一般我们继承这个类 然后复写里面的onAccessibilityEvent(AccessibilityEvent event)和onInterrupt()方法
第一个方法是监听事件用的 一旦系统监听到有改变 就会将这个event发送到这个方法中
第二个方法是当Service突然中止的时候调用
这个类我们要注意一下方法:
getRootInActiveWindow() 返回Windows改变的根布局 有了这个根布局 我们就可以通过循环递归来检索内容
onAccessibilityEvent(AccessibilityEvent event) 每当有事件改变 都会调用这个函数
performGlobalAction(int action) 这个是模拟屏幕点击
有很多的action值 比如 GLOBAL_ACTION_BACK (返回) GLOBAL_ACTION_HOME(返回home界面)
AccessibilityNodeInfo
这个类是非常有用的 他可以寻找得到界面上你想找的控件比如通过文字来查找findAccessibilityNodeInfosByText(String text)
通过id来查找 findAccessibilityNodeInfosByViewId(String viewId)
getChild(int index) 得到自己的孩子
getParent() 得到父亲
|
等等 这个类非常重要 要重点研究
第四个类是:
AccessibilityEvent
这个类封装了 事件 一些事件改变的参数都会封装在里面 比如说收到了notification 就会有个notification的事件到达 里面有它的description text这些属性摘取了部分的属性 出来看看
getEventType()
- The type of the event.getSource()
- The source info (for registered clients).getClassName()
- The class name of the source.getPackageName()
- The package name of the source.getEventTime()
- The event time.getText()
- The text of the source's sub-tree.isEnabled()
- Whether the source is enabled.isPassword()
- Whether the source is password.isChecked()
- Whether the source is checked.getContentDescription()
- The content description of the source.getScrollX()
- The offset of the source left edge in pixels (without descendants of AdapterView).getScrollY()
- The offset of the source top edge in pixels (without descendants of AdapterView).getFromIndex()
- The zero based index of the first visible item of the source, inclusive (for descendants of AdapterView).getToIndex()
- The zero based index of the last visible item of the source, inclusive (for descendants of AdapterView).getItemCount()
- The total items of the source (for descendants of AdapterView).
1、首先定义一个继承AccessibilityService 然后再Mainfest文件声明
<manifest> ... <uses-permission ... /> ... <application> ... <service android:name=".MyAccessibilityService" android:label="@string/accessibility_service_label" android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"> <intent-filter> <action android:name="android.accessibilityservice.AccessibilityService" /> </intent-filter> </service> <uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" /> </application> </manifest>其中要提供android.permission.BIND_ACCESSIBILITY_SERVICE 这个权限 然后再手机里面将这个辅助功能开启
2.配置工作
分代码配置和xml配置(其中xml配置是Android4.0才开始声明的)
代码一般在onServiceConnected下面写 例如
- private void setServiceInfo(int feedbackType) {
- AccessibilityServiceInfo info = new AccessibilityServiceInfo();
- // We are interested in all types of accessibility events.
- info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
- // We want to provide specific type of feedback.
- info.feedbackType = feedbackType;
- // We want to receive events in a certain interval.
- info.notificationTimeout = EVENT_NOTIFICATION_TIMEOUT_MILLIS;
- // We want to receive accessibility events only from certain packages.
- info.packageNames = PACKAGE_NAMES;
- setServiceInfo(info);
- }
xml文件例子如下
首先在manifest文件声明
<service android:name=".MyAccessibilityService"> ... <meta-data android:name="android.accessibilityservice" android:resource="@xml/accessibility_service_config" /> </service>然后再res新建一个xml文件夹 里面是配置的xml文件内容
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/accessibility_service_description"
android:packageNames="com.example.android.apis"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFlags="flagDefault"
android:accessibilityFeedbackType="feedbackSpoken"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity"
/>
上面的参数到底是什么意思呢?而且至少要声明多少个呢
android:description 目的的描述
android:notificationTimeout 通知隔多长时间失效
android:packageNames 包名 表情哪个应用程序
android:settingsActivity 为程序设置的Activity 设置了 会首先调到这个Activity设置
android:canRetrieveWindowContent 服务能不能访问窗口活动的内容
android:canRequestTouchExplorationMode 是否可以访问touch事件
android:accessibilityEventTypes 能够接收什么内容的Event
3.声明一个类继承
AccessibilityService 然后复写里面的两个方法
onAccessibilityEvent onInterrupt
这两个方法 其中onAccessibilityEvent 有个参数叫AccessibilityEvent事件 封装了事件传递
里面的Event有如下几大类:
View Types :单击 长按 view的选择 view申请焦点 view的文字改变 view的文字选择改变了 遍历文本 view的滚动
transition types(转换类型):window的状态改变了,比如说弹出了一个popWindow或者menu window的内容变了 window之间互相切换了
notification types:通知类型 通知栏改变了 就是有消息过来了
exploration types:悬停开始 悬停结束 等等
当然还有其他类型 反正就是Android屏幕发生的一切我们基本都可以捕捉的到呢
项目开源https://github.com/staringburning/qianghongbao