使用AccessibilityService来做一个自动抢红包插件
1 簡介
AccessibilityService中文翻譯是輔助功能,本意是幫助殘障人士來方便使用手機(jī)的功能。因?yàn)槠淇梢匀直O(jiān)聽和發(fā)送事件,所以我們可以通過這個功能來進(jìn)行一些“僭越”的操作,比如自動搶紅包,修改鍵值等。
查看Android官方網(wǎng)站關(guān)于accessibilityservice介紹,如下:
原文:
The classes in this package are used for development of accessibility service that provide alternative or augmented feedback to the user.
An AccessibilityService runs in the background and receives callbacks by the system when AccessibilityEvents are fired. Such events denote some state transition in the user interface, for example, the focus has changed, a button has been clicked, etc. Such a service can optionally request the capability for querying the content of the active window. Development of an accessibility service requires extends this class and implements its abstract methods.
An AccessibilityServiceInfo describes an AccessibilityService. The system notifies an AccessibilityService for AccessibilityEvents according to the information encapsulated in this class.
Accessibility events are messages about users interaction with visual interface components in your application. These messages are handled by Accessibility Services.
翻譯:
accessibilityservice包下的類用來開發(fā)能提供可選或擴(kuò)展的反饋給用戶的服務(wù)。
一個AccessibilityService在后臺運(yùn)行,并且在AccessibilityEvent發(fā)出時接收系統(tǒng)回調(diào)。這些事件顯示了用戶界面上某些狀態(tài)發(fā)生改變——比如焦點(diǎn)變化,按鈕點(diǎn)擊等。這個類可以查看當(dāng)前活動窗口的內(nèi)容。如果要使用這些功能,你需要繼承AccessibilityService類并實(shí)現(xiàn)其中的抽象方法。
一個AccessibilityServiceInfo描述了一個AccessibilityService。系統(tǒng)通過將信息裝在AccessibilityServiceInfo類中,通知AccessibilityService發(fā)生了AccessibilityEvent。
AccessibilityEvent是關(guān)于用戶和界面交互的信息,并由AccessibilityService來處理。
https://developer.android.google.cn/reference/android/accessibilityservice/package-summary
https://developer.android.google.cn/training/accessibility/service
2 使用
1、創(chuàng)建一個繼承自AccessibilityService的類,并在manifest中定義。
public class RedPacketService extends AccessibilityService {@Overridepublic void onAccessibilityEvent(AccessibilityEvent event) {}@Overridepublic void onInterrupt() {}}2、配置自定義的AccessibilityService。也就是說,定義何時需要執(zhí)行處理,何時不需要。
我們可以通過代碼中,或者xml文件進(jìn)行配置。這里只介紹xml配置模式,因?yàn)樵诖a中某些設(shè)置是不可用的。
創(chuàng)建redpacket_accessibility_config.xml文件如下:
<?xml version="1.0" encoding="utf-8"?> <accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"android:accessibilityEventTypes="typeViewClicked|typeViewFocused|typeNotificationStateChanged"android:canRetrieveWindowContent="true"android:notificationTimeout="100" />并在manifest中加入如下meta-data標(biāo)簽:
<service android:name=".RedPacketService"><intent-filter><action android:name="android.accessibilityservice.AccessibilityService" /></intent-filter><meta-dataandroid:name="android.accessibilityservice"android:resource="@xml/redpacket_accessibility_config" /> </service>好了,大功告成,這時候一個基本的輔助功能就實(shí)現(xiàn)了,我們可以在RedPacketService中處理事件了。
3 利用輔助功能搶紅包
總結(jié)
以上是生活随笔為你收集整理的使用AccessibilityService来做一个自动抢红包插件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python画两条曲线图_python绘
- 下一篇: java反序列化后不相等_Jackson