Android辅助功能

1:AccessibilityService

新建service

public class AccessibilityService extends android.accessibilityservice.AccessibilityService {
    private static final String TAG = "AccessibilityService";

    @Override
    public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
        Log.i(TAG, "onAccessibilityEvent: " + accessibilityEvent.getEventType());
        Log.i(TAG, "onAccessibilityEvent: event className:"+accessibilityEvent.getClassName());
    }

    @Override
    public void onInterrupt() {
        Log.i(TAG, "onInterrupt:");
    }
}

这里暂时先只添加日志输出。

2:注册service

        <service android:name=".AccessibilityService"
            android:label="@string/app_name"
            android:exported="true"
            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/phone_accessibility" />
        </service>

3: 添加resource

<?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:canPerformGestures="true"
    android:canRetrieveWindowContent="true"
    android:description="@string/accessibility_desc"
    android:notificationTimeout="100" />

4: 开启辅助功能

  Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
            startActivity(intent);

5:功能实现

这里简单的实现下打开oppo相机后的自动点击拍照。
这里可以编写onAccessibilityEvent方法中的代码了。

 Log.i(TAG, "onAccessibilityEvent: " + accessibilityEvent.getEventType());
        Log.i(TAG, "onAccessibilityEvent: event className:"+accessibilityEvent.getClassName());
        //校验版本
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2){
            return;
        }
        //获取nodeinfo
        AccessibilityNodeInfo rootInActiveWindow = getRootInActiveWindow();
        if (rootInActiveWindow==null){
            Log.i(TAG, "onAccessibilityEvent: rootInActiveWindow is null" );
            return;
        }
        //获取当前的classname, 是相机的话执行
        if (accessibilityEvent.getClassName().equals("com.oplus.camera.Camera")){
           //根据相机拍照按钮的id(monitor中获取ViewId)
            List<AccessibilityNodeInfo> shutter_button = rootInActiveWindow.findAccessibilityNodeInfosByViewId("com.oplus.camera:id/shutter_button");
            if (shutter_button!=null){
                for (AccessibilityNodeInfo accessibilityNodeInfo : shutter_button) {
                //执行点击事件
                    accessibilityNodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    Toast.makeText(getApplicationContext(),"自动点击成功",Toast.LENGTH_LONG).show();
                }
            }
        }

版权声明:本文为qq_23025319原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
THE END
< <上一篇
下一篇>>