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

        
            
                
            

            
        

3: 添加resource



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 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();
                }
            }
        }

你可能感兴趣的:(android,java,开发语言)