Accessibility 辅助功能 自动下载

来源:互联网 发布:二战日军战斗力 知乎 编辑:程序博客网 时间:2024/04/29 17:14

1、Accessibility介绍

Many Android users have different abilities that require them to interact with their Android devices in different ways. These include users who have visual, physical or age-related limitations that prevent them from fully seeing or using a touchscreen, and users with hearing loss who may not be able to perceive audible information and alerts.

Android provides accessibility features and services for helping these users navigate their devices more easily, including text-to-speech, haptic feedback, gesture navigation, trackball and directional-pad navigation. Android application developers can take advantage of these services to make their applications more accessible.

(自己理解,具体参考百度)
Android用户的能力不同,需要通过不同的方式来使用Android设备。由于视力、身体和年龄因素的影响,会让这些人(盲人等)无法感知到信息和警报。
Android提供了accessibility服务(文字转语音、触觉反馈、手势导航、轨迹球和方向键导航),帮助这些人更方便的使用Android设备。而Android的开发者,可以利用这项服务,使得他们的应用更容易。

2、具体的用途

利用这项服务,当用户开启了这项服务后,除了上述的功能,还可以实现软件的自动安装、模拟用户在其他应用上的点击。

3、使用的方法

<uses-permissionandroid:name="android.permission.BIND_ACCESSIBILITY_SERVICE"/>

在AndroidManifest.xml中声明权限,用户请求accessbility服务,并注册服务



phone_accessibility.xml

<?xmlversion="1.0"encoding="utf-8"?>

<accessibility-servicexmlns:android="http://schemas.android.com/apk/res/android"

    android:accessibilityEventTypes="typeAllMask"

    android:accessibilityFeedbackType="feedbackGeneric"

    android:accessibilityFlags=""

    android:canRetrieveWindowContent="true"

    android:description="@string/accessibility_service_description"

    android:notificationTimeout="100"/>


在代码中,申请开启服务

Intent accessibilityIntent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);

startActivity(accessibilityIntent);


创建服务,继承AccessibilityService,监听事件

@Override

public void onAccessibilityEvent(AccessibilityEvent event) {

List<AccessibilityNodeInfo> installNodes =event.getSource().findAccessibilityNodeInfosByText("安装吧");

if(installNodes!=null && !installNodes.isEmpty()){

for(AccessibilityNodeInfo info : installNodes){

AccessibilityNodeInfo parentInfo = info.getParent();

while(parentInfo!=null){

parentInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);parentInfo = parentInfo.getParent();

}

info.performAction(AccessibilityNodeInfo.ACTION_CLICK);

}

}

}


注意:onAccessibilityEvent一开始尽量做包名判断的处理,否则执行过多,会让程序崩溃;


参考的博客:http://www.infoq.com/cn/articles/android-accessibility-installing?utm_campaign=infoq_content&utm_source=infoq&utm_medium=feed&utm_term=global


0 0
原创粉丝点击