使用辅助功能

时间:2018-06-05 05:56:59

标签: android accessibility accessibilityservice android-accessibility accessibility-api

我正在使用辅助功能服务来获取视图的数据。为此,我有一个“电源按钮”按钮,点击该按钮可显示用户关闭/重启手机的选项。

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/power"
            android:contentDescription="bleh"
            android:text="@string/power"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>

我的服务类扩展了辅助功能服务类并以这种方式覆盖了方法

package com.example.aayushi.myapplication;
public class MyAccessibilityService extends AccessibilityService {
    FrameLayout mLayout; //9742612318

    @Override
    protected void onServiceConnected() {
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        mLayout = new FrameLayout(this);
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.type = WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;
        lp.format = PixelFormat.TRANSLUCENT;
        lp.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        LayoutInflater inflater = LayoutInflater.from(this);
        inflater.inflate(R.layout.activity_main, mLayout);
        wm.addView(mLayout, lp);
        configurePowerButton();
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        AccessibilityNodeInfo source = event.getSource();//EventType: TYPE_WINDOW_STATE_CHANGED
        if(source==null)return;
        Log.i("source--------", source.toString());
        List<AccessibilityNodeInfo> nodeInfo = source.findAccessibilityNodeInfosByViewId("com.example.aayushi.myapplication:id/power");
        if (nodeInfo.size()>0) {
            AccessibilityNodeInfo parent = nodeInfo.get(0);
            // You can also traverse the list if required data is deep in view hierarchy.
            String requiredText = parent.getText().toString();
            Log.i("Required Text", requiredText);
        }
    }

    @Override
    public void onInterrupt() {

    }

    private void configurePowerButton() {
        Button powerButton = (Button) mLayout.findViewById(R.id.power);
        powerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                performGlobalAction(GLOBAL_ACTION_POWER_DIALOG);
            }
        });
    }
}

在这里,我将电源按钮的id传递给findAccessibilityNodeInfosByViewId方法。我想, 我应该能够在源代码中获取文本和contentDescription的值。但相反,我得到了空。 Log.i(“”,source)就是这样 -

我/源--------:android.view.accessibility.AccessibilityNodeInfo@80006cbe; boundsInParent:Rect(0,0-1080,1920); boundsInScreen:Rect(0,0 - 1080,1920); packageName:com.android.launcher3; className:android.widget.FrameLayout; text:null;错误:null; maxTextLength:-1; contentDescription:null; viewIdResName:null;可检查的:假的;检查:false;焦点:虚假;专注:虚假;选中:false;可点击的:false; longClickable:false; contextClickable:false;启用:true;密码:false;可滚动的:false; importantForAccessibility:true;操作:[AccessibilityAction:ACTION_SELECT - null,AccessibilityAction:ACTION_CLEAR_SELECTION - null,AccessibilityAction:ACTION_ACCESSIBILITY_FOCUS - null,AccessibilityAction:ACTION_SHOW_ON_SCREEN - null]

这里有什么问题?

0 个答案:

没有答案
相关问题