小视频app源码无障碍服务实现自动跳过APP启动页广告
小视频app源码无障碍服务实现自动跳过APP启动页广告实现的相关代码
一. res目录下新建xml文件夹新建文件accessibility.xml
<?xml version="1.0" encoding="utf-8"?> <accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" android:accessibilityEventTypes="typeWindowStateChanged|typeNotificationStateChanged|typeViewTextSelectionChanged|typeViewClicked" android:accessibilityFeedbackType="feedbackGeneric" android:canRetrieveWindowContent="true" android:accessibilityFlags="flagDefault|flagRetrieveInteractiveWindows" android:canRequestTouchExplorationMode="true" android:description="@string/app_name" android:notificationTimeout="100"/>
清单文件中service中配置引用文件
<service android:name=".SkipService" android:label="Automaticskip" 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/accessibility" /> </service>
清单文件权限如下:
<uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
二. 新建服务SkipService继承AccessibilityService类
package com.androidlmy.automaticskip; import android.accessibilityservice.AccessibilityService; import android.accessibilityservice.AccessibilityServiceInfo; import android.content.ComponentName; import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; import android.os.Build; import android.os.Handler; import android.util.Log; import android.view.accessibility.AccessibilityEvent; import android.view.accessibility.AccessibilityNodeInfo; import android.widget.Toast; import java.util.List; /** * @功能: * @Creat 2019/12/2 10:16 * @User Lmy * @Compony zaituvideo */ public class SkipService extends AccessibilityService { @Override protected void onServiceConnected() { super.onServiceConnected(); AccessibilityServiceInfo config = new AccessibilityServiceInfo(); //配置监听的事件类型为界面变化|点击事件 config.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED | AccessibilityEvent.TYPE_VIEW_CLICKED; config.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC; if (Build.VERSION.SDK_INT >= 16) { config.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS; } setServiceInfo(config); } @Override public void onAccessibilityEvent(AccessibilityEvent event) { final AccessibilityNodeInfo nodeInfo = event.getSource();//当前界面的可访问节点信息 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {//界面变化事件 ComponentName componentName = new ComponentName(event.getPackageName().toString(), event.getClassName().toString()); ActivityInfo activityInfo = tryGetActivity(componentName); boolean isActivity = activityInfo != null; if (isActivity) { Log.d("WindowChange", "当前运行包名" + nodeInfo.getPackageName()); switch (nodeInfo.getPackageName().toString()) { case "com.netease.cloudmusic": new Handler().postDelayed(new Runnable() { @Override public void run() { skip(nodeInfo.findAccessibilityNodeInfosByViewId("com.netease.cloudmusic:id/c3l")); } }, 500); break; case "cn.xiaochuankeji.zuiyouLite": new Handler().postDelayed(new Runnable() { @Override public void run() { skip(nodeInfo.findAccessibilityNodeInfosByViewId("cn.xiaochuankeji.zuiyouLite:id/btn_skip")); } }, 2000); break; default: { List<AccessibilityNodeInfo> nodeInfoList = nodeInfo.findAccessibilityNodeInfosByText("跳过"); for (AccessibilityNodeInfo info : nodeInfoList) { CharSequence charSequence = info.getText(); if (charSequence != null) { String msg = charSequence.toString(); if (msg.contains("跳过")) { info.performAction(AccessibilityNodeInfo.ACTION_CLICK); Toast.makeText(this, "跳过广告", Toast.LENGTH_SHORT).show(); } } } } } } } } private void skip(List<AccessibilityNodeInfo> nodeInfoList) { Log.d("WindowChange", "数据" + nodeInfoList.size()); if (nodeInfoList != null && nodeInfoList.size() > 0) { nodeInfoList.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK); Toast.makeText(getApplicationContext(), "跳过广告", Toast.LENGTH_SHORT).show(); } } private ActivityInfo tryGetActivity(ComponentName componentName) { try { return getPackageManager().getActivityInfo(componentName, 0); } catch (PackageManager.NameNotFoundException e) { return null; } } @Override public void onInterrupt() { } }
主要代码
nodeInfo.findAccessibilityNodeInfosByViewId("com.netease.cloudmusic:id/c3l")
根据已知控件id查找网易云启动页的控件 下面会介绍如何获取控件的id
private void skip(List<AccessibilityNodeInfo> nodeInfoList) { Log.d("WindowChange", "数据" + nodeInfoList.size()); if (nodeInfoList != null && nodeInfoList.size() > 0) { nodeInfoList.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK); Toast.makeText(getApplicationContext(), "跳过广告", Toast.LENGTH_SHORT).show(); } } nodeInfoList.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
以上就是小视频app源码无障碍服务实现自动跳过APP启动页广告实现的相关代码, 更多内容欢迎关注之后的文章