How to skip the opening screen advertisement of the Nuggets app (2)

This is the 24th day of my participation in the August Update Challenge. For details of the event, please check: August Update Challenge

How to skip the opening screen advertisement of the Nuggets app (2)

The last article analyzed that the core of wanting to skip the open screen advertisement is that the mobile phone helps us click 跳过the button, and finally selected the accessibility service as our technology. This article will implement the code for skipping the advertisement function.

1. Effect

Programmatic clicks are 跳过much faster than manual clicks.

1629623520219210.gif

Second, the code implementation

2.1 Key callbacks

After the created service inheritance AccessibilityServiceclass, another key callback is the onAccessibilityEventcallback, and our logic is all in this method.

 @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
}
复制代码

2.1 Locating the package name

We only want to skip ads in a certain app or under certain apps.

two methods

  1. Add the application package name to the android:packageNamesattribute
  2. Get the package name in the service and make a judgment.
String packageStr = event.getPackageName().toString();

if (TextUtils.equals(packageStr, "com.xx.xxx")) {
        
}
复制代码

For example, the package name of the obtained Nuggets app iscom.daimajia.gold

ps: I searched on the Internet 代码家, and github shows that this person is the CTO of Nuggets.

2.2 Locate the target component

If you want to add a click method to a component, you must first locate the component, get the component, and then add a click action.

How to locate components?

There are three methods I use here.

  1. uiautomator, comes with Android sdk, can be easily used
  2. autoJs, which is an app, also uses accessibility services. Based on accessibility services, a graphical interface is added for easy viewing on the mobile phone.
  3. Use the code to traverse and view it yourself.

detailed analysis:

There is a "skip" text in the upper right corner of the Nuggets app to analyze the view structure, which is probably the case.

Fake code

<FragmentLayout id:fl_skip > // 实际点击事件在这个

<ProgressBar >  // 这个是跳过效果的进度

<TextView text="跳过"> // 跳过的文字载体

</FragmentLayout>
复制代码

2.3 Get the target component

First get all components of the current page -> then get the target component

Use getRootInActiveWindow()to get all components
Use findAccessibilityNodeInfosByViewId(“”)to get all NodeLists that match this ID Use findAccessibilityNodeInfosByText()to get all NodeLists that match this text

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);
            }
            
复制代码

2.4 Set click action

After finding the location of the target AccessibilityNodeInfo, we start the operation.

Commonly used are

ACTION_CLICK: Action to click on node information. ACTION_LONG_CLICK: Click and hold the action on the node. ACTION_COPY: The operation to copy the current selection to the clipboard. ACTION_CUT: Cut and paste the current option and place it on the clipboard. ACTION_FOCUS: The operation of adding input focus to the node.

We can implement AccessibilityNodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);it to achieve the effect.

2.5 Code Section

If you want to implement the functions of other apps, you can expand the code.

full 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() {

    }

}

复制代码

Guess you like

Origin juejin.im/post/6999807957118484516