关闭

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

标签: android安全
696人阅读 评论(0) 收藏 举报
分类:

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:11953次
    • 积分:368
    • 等级:
    • 排名:千里之外
    • 原创:24篇
    • 转载:1篇
    • 译文:1篇
    • 评论:0条