使用AccessibilityService来做一个自动抢红包插件
1 簡介
AccessibilityService中文翻譯是輔助功能,本意是幫助殘障人士來方便使用手機的功能。因為其可以全局監聽和發送事件,所以我們可以通過這個功能來進行一些“僭越”的操作,比如自動搶紅包,修改鍵值等。
查看Android官方網站關于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包下的類用來開發能提供可選或擴展的反饋給用戶的服務。
一個AccessibilityService在后臺運行,并且在AccessibilityEvent發出時接收系統回調。這些事件顯示了用戶界面上某些狀態發生改變——比如焦點變化,按鈕點擊等。這個類可以查看當前活動窗口的內容。如果要使用這些功能,你需要繼承AccessibilityService類并實現其中的抽象方法。
一個AccessibilityServiceInfo描述了一個AccessibilityService。系統通過將信息裝在AccessibilityServiceInfo類中,通知AccessibilityService發生了AccessibilityEvent。
AccessibilityEvent是關于用戶和界面交互的信息,并由AccessibilityService來處理。
https://developer.android.google.cn/reference/android/accessibilityservice/package-summary
https://developer.android.google.cn/training/accessibility/service
2 使用
1、創建一個繼承自AccessibilityService的類,并在manifest中定義。
public class RedPacketService extends AccessibilityService {@Overridepublic void onAccessibilityEvent(AccessibilityEvent event) {}@Overridepublic void onInterrupt() {}}2、配置自定義的AccessibilityService。也就是說,定義何時需要執行處理,何時不需要。
我們可以通過代碼中,或者xml文件進行配置。這里只介紹xml配置模式,因為在代碼中某些設置是不可用的。
創建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標簽:
<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>好了,大功告成,這時候一個基本的輔助功能就實現了,我們可以在RedPacketService中處理事件了。
3 利用輔助功能搶紅包
總結
以上是生活随笔為你收集整理的使用AccessibilityService来做一个自动抢红包插件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python画两条曲线图_python绘
- 下一篇: java反序列化后不相等_Jackson