借助Android的无障碍服务,实现打开App自动跳过广告
class AdSkipAccessibilityService: AccessibilityService() { override fun onAccessibilityEvent(event: AccessibilityEvent?) { val source = event?.source ?: return for (i in 0 until source.childCount) { if (source.getChild(i)?.text?.contains("跳过") == true) { source.getChild(i).performAction(ACTION_CLICK) } } source.recycle() }
override fun onInterrupt() {
} }
|
在AndroidManifest.xml注册服务
<service android:name=".AdSkipAccessibilityService" android:exported="false" android:label="开屏跳" 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>
|
在 res/xml
下新建一个serviceconfig.xml
<?xml version="1.0" encoding="utf-8"?> <accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" android:accessibilityEventTypes="typeAllMask" android:accessibilityFeedbackType="feedbackGeneric" android:accessibilityFlags="flagDefault" android:canPerformGestures="true" android:canRetrieveWindowContent="true" android:description="@string/accessibility_service_description" android:notificationTimeout="100" android:settingsActivity="com.carl.adskip.MainActivity" />
|
跳转到无障碍服务设置页面
startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS))
|
打开设置后,这个App需要保活,不然服务会关闭。