Android无障碍服务

Posted 英文不好太难了

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android无障碍服务相关的知识,希望对你有一定的参考价值。

新建BaseAccessibilityService 类

public class BaseAccessibilityService extends AccessibilityService 

    private AccessibilityManager mAccessibilityManager;
    private Context mContext;
    private static BaseAccessibilityService mInstance;

    public void init(Context context) 
        mContext = context.getApplicationContext();
        mAccessibilityManager = (AccessibilityManager) mContext.getSystemService(Context.ACCESSIBILITY_SERVICE);
    

    public static BaseAccessibilityService getInstance() 
        if (mInstance == null) 
            mInstance = new BaseAccessibilityService();
        
        return mInstance;
    

    /**
     * Check当前辅助服务是否启用
     *
     * @param serviceName serviceName
     * @return 是否启用
     */
    private boolean checkAccessibilityEnabled(String serviceName) 
        List<AccessibilityServiceInfo> accessibilityServices =
                mAccessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_GENERIC);
        for (AccessibilityServiceInfo info : accessibilityServices) 
            if (info.getId().equals(serviceName)) 
                return true;
            
        
        return false;
    

    /**
     * 前往开启辅助服务界面
     */
    public void goAccess() 
        Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
    

    /**
     * 模拟点击事件
     *
     * @param nodeInfo nodeInfo
     */
    public void performViewClick(AccessibilityNodeInfo nodeInfo) 
        if (nodeInfo == null) 
            return;
        
        while (nodeInfo != null) 
            if (nodeInfo.isClickable()) 
                nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                break;
            
            nodeInfo = nodeInfo.getParent();
        
    

    /**
     * 模拟返回操作
     */
    public void performBackClick() 
        try 
            Thread.sleep(500);
         catch (InterruptedException e) 
            e.printStackTrace();
        
        performGlobalAction(GLOBAL_ACTION_BACK);
    

    /**
     * 模拟下滑操作
     */
    public void performScrollBackward() 
        try 
            Thread.sleep(500);
         catch (InterruptedException e) 
            e.printStackTrace();
        
        performGlobalAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
    

    /**
     * 模拟上滑操作
     */
    @RequiresApi(api = Build.VERSION_CODES.N)
    public void performScrollForward() 
        try 
            Thread.sleep(500);
         catch (InterruptedException e) 
            e.printStackTrace();
        
        performGlobalAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);

    

    /**
     * 查找对应文本的View
     *
     * @param text text
     * @return View
     */
    public AccessibilityNodeInfo findViewByText(String text) 
        return findViewByText(text, false);
    

    /**
     * 查找对应文本的View
     *
     * @param text      text
     * @param clickable 该View是否可以点击
     * @return View
     */
    public AccessibilityNodeInfo findViewByText(String text, boolean clickable) 
        AccessibilityNodeInfo accessibilityNodeInfo = getRootInActiveWindow();
        if (accessibilityNodeInfo == null) 
            return null;
        
        List<AccessibilityNodeInfo> nodeInfoList = accessibilityNodeInfo.findAccessibilityNodeInfosByText(text);
        if (nodeInfoList != null && !nodeInfoList.isEmpty()) 
            for (AccessibilityNodeInfo nodeInfo : nodeInfoList) 
                if (nodeInfo != null && (nodeInfo.isClickable() == clickable)) 
                    return nodeInfo;
                
            
        
        return null;
    

    /**
     * 查找对应ID的View
     *
     * @param id id
     * @return View
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    public AccessibilityNodeInfo findViewByID(String id) 
        AccessibilityNodeInfo accessibilityNodeInfo = getRootInActiveWindow();
        if (accessibilityNodeInfo == null) 
            return null;
        
        List<AccessibilityNodeInfo> nodeInfoList = accessibilityNodeInfo.findAccessibilityNodeInfosByViewId(id);
        if (nodeInfoList != null && !nodeInfoList.isEmpty()) 
            Log.d("dd", "findViewByID: " + nodeInfoList.size());
            for (AccessibilityNodeInfo nodeInfo : nodeInfoList) 
                Log.d("dd", "findViewByID: " + nodeInfo.toString());
                if (nodeInfo != null) 
                    return nodeInfo;
                
            
        
        return null;
    

    public void clickTextViewByText(String text) 
        AccessibilityNodeInfo accessibilityNodeInfo = getRootInActiveWindow();
        if (accessibilityNodeInfo == null) 
            return;
        
        List<AccessibilityNodeInfo> nodeInfoList = accessibilityNodeInfo.findAccessibilityNodeInfosByText(text);
        if (nodeInfoList != null && !nodeInfoList.isEmpty()) 
            for (AccessibilityNodeInfo nodeInfo : nodeInfoList) 
                if (nodeInfo != null) 
                    performViewClick(nodeInfo);
                    break;
                
            
        
    

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    public void clickTextViewByID(String id) 
        AccessibilityNodeInfo accessibilityNodeInfo = getRootInActiveWindow();
        if (accessibilityNodeInfo == null) 
            return;
        
        List<AccessibilityNodeInfo> nodeInfoList = accessibilityNodeInfo.findAccessibilityNodeInfosByViewId(id);
        if (nodeInfoList != null && !nodeInfoList.isEmpty()) 
            for (AccessibilityNodeInfo nodeInfo : nodeInfoList) 
                if (nodeInfo != null) 
                    performViewClick(nodeInfo);
                    break;
                
            
        
    

    /**
     * 模拟输入
     *
     * @param nodeInfo nodeInfo
     * @param text     text
     */
    public void inputText(AccessibilityNodeInfo nodeInfo, String text) 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
            Bundle arguments = new Bundle();
            arguments.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, text);
            nodeInfo.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);
         else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) 
            ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clip = ClipData.newPlainText("label", text);
            clipboard.setPrimaryClip(clip);
            nodeInfo.performAction(AccessibilityNodeInfo.ACTION_FOCUS);
            nodeInfo.performAction(AccessibilityNodeInfo.ACTION_PASTE);
        
    

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) 
        Log.d("dd", "onAccessibilityEvent: " + event.toString());
    

    @Override
    public void onInterrupt() 
    

    @Override
    protected void onServiceConnected() 
        super.onServiceConnected();
        Log.d("llll", "onServiceConnected: ");
    



开启无障碍方法
public void accessibility() 
BaseAccessibilityService baseAccessibilityService = new BaseAccessibilityService();
 //初始化
baseAccessibilityService.init(this);
baseAccessibilityService.goAccess();
//    

以上是关于Android无障碍服务的主要内容,如果未能解决你的问题,请参考以下文章

Android Accessibility无障碍服务安全性浅析

Android Accessibility无障碍服务安全性浅析

Android用无障碍服务整个脚本——我看刑

Android用无障碍服务整个脚本——我看刑

Android无障碍服务

Android无障碍服务