Android Accessibility API的研究与应用(一)

Android accessibility Api通常都是用作一些辅助功能,如点击等。但是由于权限问题,存在一定安全上面的问题,例如获取手机屏幕上的信息内容等,下面是官方对AccessibilityEvent的称述:

This class represents accessibility events that are sent by the system when something notable happens in the user interface. For example, when a Button is clicked, a View is focused, etc.

An accessibility event is fired by an individual view which populates the event with data for its state and requests from its parent to send the event to interested parties. The parent can optionally add an AccessibilityRecordfor itself before dispatching a similar request to its parent. A parent can also choose not to respect the request for sending an event. The accessibility event is sent by the topmost view in the view tree. Therefore, an AccessibilityService can explore all records in an accessibility event to obtain more information about the context in which the event was fired.

The main purpose of an accessibility event is to expose enough information for an AccessibilityService to provide meaningful feedback to the user. Sometimes however, an accessibility service may need more contextual information then the one in the event pay-load. In such cases the service can obtain the event source which is an AccessibilityNodeInfo (snapshot of a View state) which can be used for exploring the window content. Note that the privilege for accessing an event's source, thus the window content, has to be explicitly requested. For more details refer to AccessibilityService. If an accessibility service has not requested to retrieve the window content the event will not contain reference to its source. Also for events of typeTYPE_NOTIFICATION_STATE_CHANGED the source is never available.

简单地说,可以通过重载onAccessibilityEvent,获得一个AccessibilityEvent对象,而这个对象可以的到所有的通过accessibility service暴露出来的信息,这些信息包括获得当前系统激活应用的包名(描述可能不太准确),获得当前界面的信息,得到当前用户的动作等功能.

部分代码如下

public class MyAccessibilityService extends AccessibilityService {


    @Override

    public void onAccessibilityEvent(AccessibilityEvent event) {

    Log.i("PackageName",event.getPackageName().toString());

    Log.d("EventName",getTypeName(event.getEventType()));

    

    traverseNode(getRootInActiveWindow());

    

    AccessibilityNodeInfo nodes = event.getSource();  

    

    Log.d("------------------------","-------------------------");

    }


    @Override

    public void onInterrupt() {

    }



    private void traverseNode(AccessibilityNodeInfo node) {

        if (null == node)

            return;


        final int count = node.getChildCount();

        if (count > 0) {

            for (int i = 0; i < count; i++) {

                AccessibilityNodeInfo childNode = node.getChild(i);

                traverseNode(childNode);

            }

        } else {

            CharSequence text = node.getText();

            Log.d("test", "Node text = " + text);

        }

        

    }

}

待解决的问题:

系统是合适调用onAccessibilityEvent的,经过试验表明,不同界面切换,同一界面上输入内容时,会激活;但是诸如在系统菜单左右滑动页面,没有任何反应

注:本人刚学习Android,用词上可能存在一定的不恰当,请各位批评与包容

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值