This is the 24th day of my participation in the August Text Challenge.More challenges in August
How to skip the opening screen advertisement of nuggets App (2)
According to the analysis in the previous article, the core of skipping open screen ads is that mobile phones help us click the skip button. Finally, barrier-free services are selected as our technology. This article will implement the code of skipping ads.
A, effects,
Programmatically click-skip is much faster than clicking manually.
Two, code implementation
2.1 Key callback
After creating a service that inherits the AccessibilityService class, the next key callback is the onAccessibilityEvent callback, where all our logic resides.
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
}
Copy the code
2.1 Locating package names
We only want to skip ads in one app or some apps.
The two methods
- In the XML config file
android:packageNames
Property to add the application package name - Get the package name in the service to determine.
String packageStr = event.getPackageName().toString();
if (TextUtils.equals(packageStr, "com.xx.xxx")) {
}
Copy the code
For example, the package name of the gold digging app is com.daimajia. Gold
Ps: Do a quick Internet search for coders and github lists him as the Nuggets’ CTO.
2.2 Locating Target Components
To add a click method to a component, first locate the component, get the component, and then add the click action.
How do you locate components?
There are three commonly used methods here.
- Uiautomator, which comes with the Android SDK, can be used easily
- AutoJs, an app, also uses barrier-free services. Based on barrier-free services, a graphical interface is added, which can be viewed on the mobile phone.
- Walk through the code yourself.
Specific analysis:
The Nuggets app has “skip” text in the upper right corner, analyzing view structure, something like this.
Pseudo code
<FragmentLayout ID :fl_skip > // The actual click event on the <ProgressBar > // This is the progress of the skip effect <TextView text=" skip "> // The skip text carrier </FragmentLayout>Copy the code
2.3 Obtaining Target Components
First get all the components of the current page -> then get the target component
Use the getRootInActiveWindow () for all components using findAccessibilityNodeInfosByViewId (” “) for all comply with the NodeList that ID Using findAccessibilityNodeInfosByText NodeList () for all the words
AccessibilityNodeInfo sourceNodeInfo = getRootInActiveWindow();
if (sourceNodeInfo == null)
return;
List<AccessibilityNodeInfo> textNodeInfoList = sourceNodeInfo.findAccessibilityNodeInfosByViewId("com.daimajia.gold:id/fl_skip");
if (textNodeInfoList.size() > 0) {
textNodeInfoList.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
}
Copy the code
2.4 Setting click operation
After finding the AccessibilityNodeInfo for the location of the target, we proceed.
Commonly used with
ACTION_CLICK: Action of clicking on node information. ACTION_LONG_CLICK: Click and hold on a node. ACTION_COPY: The action of copying the current selection to the clipboard. ACTION_CUT: The action of cutting and pasting the current option to the clipboard. ACTION_FOCUS: Operation of adding input focus to a node.
We perform AccessibilityNodeInfo. PerformAction (AccessibilityNodeInfo. ACTION_CLICK); You can do that.
2.5 Code Section
If you want to achieve the functions of other apps, you can extend the code.
Complete code.
package com.demo.accessibilitydemo;
import android.accessibilityservice.AccessibilityService;
import android.text.TextUtils;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import java.util.List;
public class MyService extends AccessibilityService {
private final String TAG = "MyService";
public MyService() {
}
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
Log.d(TAG, "package:" + event.getPackageName());
String packageStr = event.getPackageName().toString();
if (TextUtils.equals(packageStr, "com.daimajia.gold")) {
AccessibilityNodeInfo sourceNodeInfo = getRootInActiveWindow();
if (sourceNodeInfo == null)
return;
List<AccessibilityNodeInfo> textNodeInfoList = sourceNodeInfo.findAccessibilityNodeInfosByViewId("com.daimajia.gold:id/fl_skip");
if (textNodeInfoList.size() > 0) {
textNodeInfoList.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
}
}
}
@Override
public void onInterrupt() {
}
}
Copy the code