android开发 无障碍开发的基本使用

使用场景

自动抢红包,一键自动安装app等

基本使用

创建AccessibilityService。

public class MyAccessibilityService extends AccessibilityService {


    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
    }

    @Override
    public void onInterrupt() {

    }
}

onAccessibilityEvent
当系统检测到与无障碍服务指定的事件过滤参数匹配的 AccessibilityEvent 时(例如,当用户点按某个按钮,无障碍服务正在为其提供反馈的应用中的界面控件时,系统会回调此方法。)当系统调用此方法时,会传递关联的 AccessibilityEvent,服务随后可以解读并使用它向用户提供反馈。此方法可以在服务的整个生命周期内调用多次。

主要功能在onAccessibilityEvent方法中完成。

无障碍服务声明

设置permission为android.permission.BIND_ACCESSIBILITY_SERVICE
添加无障碍服务 intent 过滤器,android.accessibilityservice.AccessibilityService。

 	<application
        	.............>

        <service
            android:name=".service.MyAccessibilityService"
            android:enabled="true"
            android:exported="true"
            android:label="我是无障碍"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
            <intent-filter>
                <action android:name="android.accessibilityservice.AccessibilityService" />
            </intent-filter>
            <meta-data
                android:name="android.accessibilityservice"
                android:resource="@xml/accessibility_service_config" />
        </service>
    </application>

无障碍服务配置accessibility_service_config

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeWindowContentChanged|typeViewFocused"
    android:accessibilityFeedbackType="feedbackGeneric"
    android:accessibilityFlags="flagReportViewIds|flagIncludeNotImportantViews"
    android:canPerformGestures="true"
    android:canRetrieveWindowContent="true"
    android:description="@string/click_window_screen"
    android:notificationTimeout="100"
    android:packageNames="com.xx.abc" />

android:accessibilityEventTypes 监听的事件类型
typeWindowContentChanged窗口内容改变
typeViewFocused 控件获取焦点

accessibilityFeedbackType 指定无障碍服务的反馈方式

canPerformGestures 是否允许手势

canRetrieveWindowContent 是否能够获取读取窗口中的节点和内容

notificationTimeout 两个相同类型事件发送的时间间隔,单位毫秒

accessibilityFlags 指定额外的标志
flagReportViewIds 获取view的id
flagIncludeNotImportantViews 包含不重要的view

android:description 可在使用服务中看到的名称
android:packageNames 需要监听的app包名

详细信息请看:

https://developer.android.google.cn/guide/topics/ui/accessibility/service?hl=zh-cn

点击和输入

点击

    private void clickView(AccessibilityNodeInfo accessibilityNodeInfo) {

        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {//

            accessibilityNodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
        } else {

            Rect bounds = new Rect();
            accessibilityNodeInfo.getBoundsInScreen(bounds);

            Path path = new Path();
            path.moveTo((float) (bounds.left + bounds.right) / 2, (float) (bounds.top + bounds.bottom) / 2);
            dispatchGesture(new GestureDescription.Builder()
                    .addStroke(new GestureDescription.StrokeDescription(path, 0, 200)).build(), null, null);
        }
    }

输入文字

  Bundle arguments = new Bundle();
  arguments.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, "123456");
  accessibilityNodeInfo.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);

检测是否开启无障碍模式

serviceName要传完成的包名,例如:com.sd.other.service.MyAccessibilityService

  public boolean isAccessibilityServiceEnabled(Context context, String serviceName) {
        int accessibilityEnabled = 0;
        final String service = context.getPackageName() + "/" + serviceName;
        try {
            accessibilityEnabled = Settings.Secure.getInt(
                    context.getContentResolver(),
                    Settings.Secure.ACCESSIBILITY_ENABLED);

            Timber.d("accessibilityEnabled = " + accessibilityEnabled);

            if (accessibilityEnabled == 1) {
                TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');

                String settingValue = Settings.Secure.getString(
                        context.getContentResolver(),
                        Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);

                if (settingValue != null) {
                    mStringColonSplitter.setString(settingValue);
                    while (mStringColonSplitter.hasNext()) {
                        String accessibilityService = mStringColonSplitter.next();

                        Timber.d("accessibilityService = " + accessibilityService);

                        if (accessibilityService.equalsIgnoreCase(service)) {
                            return true;
                        }
                    }
                }
            } else {
                Timber.d("ACCESSIBILITY_ENABLED - NOT");
            }
        } catch (Settings.SettingNotFoundException e) {
            Timber.d("Error finding setting" + e.toString());
        }
        return false;
    }

跳转无障碍设置页面

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
         Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
         startActivity(intent);
 } else {
        Intent intent = new Intent(Settings.ACTION_SETTINGS);
        startActivity(intent);
 }

使用无障碍功能

在手机系统设置页面,找到无障碍功能,找到自己开发的,然后设置为打开,才能正常的工作。

其他

1.开了辅助app还是经常被杀死
开启任务栏锁定+电池优化白名单
vivo:一情况下,在电池设置里面有个高耗电应用
小米(红米):应用设置页面,省电策略-无限制
2.无障碍列表中找不到自己无障碍
卸载重装下试试
3.自动安装app
要开启悬浮窗权限。动态注册广播注册接收器,为什么动态,因为在某些版本中静态注册,app在后台不能接受到安装完成的广播,就触发不了安装下一个软件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值