AccessibilityService
监听其他屏幕view视图及事件及信息,模拟事件,可用于抢红包,测试等。
开启无障碍服务,到无障碍设置页面开启app无障碍设置
不同的手机厂商开启不一样
//代码跳转设置无障碍
startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
原理:app继承AccessibilityService
会注册到AccessibilityManager
VIew 源码中很多事件都会发事件发送到AccessibilityManager ,AccessibilityManager 然后通知继承的service,从而得到其他app的事件,及view信息。
sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
public boolean performClick() {
6589 // We still need to call this method to handle the cases where performClick() was called
6590 // externally, instead of through performClickInternal()
6591 notifyAutofillManagerOnClick();
6592
6593 final boolean result;
6594 final ListenerInfo li = mListenerInfo;
6595 if (li != null && li.mOnClickListener != null) {
6596 playSoundEffect(SoundEffectConstants.CLICK);
6597 li.mOnClickListener.onClick(this);
6598 result = true;
6599 } else {
6600 result = false;
6601 }
6602
6603 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
6604
6605 notifyEnterOrExitForAutoFillIfNeeded(true);
6606
6607 return result;
6608 }
继承AccessibilityService
监听事件
<service android:name=".MyAccessibilityService"
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="@xml/serviceconfig" />
</service>