在 Android 台设备上以编程方式 enabling/disabling 辅助功能设置

Programmatically enabling/disabling accessibility settings on Android device

如何以编程方式enable/disable android 屏幕 reader 服务,例如 TalkBack?

我正在开发一个信息亭类型的应用程序,它将安装在一个 Android 设备上,该设备将在参观特定博物馆时借给游客。 (我们仍在确定我们将使用的设备。)计划是只允许用户使用我们的应用程序,而不能访问 android 设置应用程序。但是,我们希望允许用户配置一些辅助功能设置。当他们使用完设备后,我们需要将所有设置恢复为默认设置。

下面 link 的讨论有很多建议启动 Android 的设置应用程序。但我们不希望用户访问许多其他设置。

How to Programmatically Enable/Disable Accessibility Service in Android

只有系统应用才能以编程方式enable/disable 无障碍服务。 系统应用可以直接在settings secure db中写入启动accessibility service

Settings.Secure.putString(getContentResolver(),Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, "com.packagename/com.packagename.componentname");

写入设置安全数据库需要以下权限:

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

对于非系统应用程序,启动辅助功能服务的唯一方法是通过 Intent 将它们引导至辅助功能设置屏幕,然后让用户手动启动该服务:

Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);

From lollipop you can not change all settings that breaks the security policy. Some of them you may access but you have to take permission for that. So please don't waste time on that.

我认为如果您将您的应用设为 AccessibilityService(但您必须在安装后手动启用它)。

然后在您的 AccessibilityService class 中,在 onAccessibilityEvent 方法中您可以浏览视图(递归)并执行点击 - 在下面的示例中,它将点击设置中的 TalkBack 项目 - 之后它应该在下一个屏幕上切换切换按钮(诀窍是您可以单击父级而不是切换视图本身)-我没有尝试过此代码:)

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    AccessibilityNodeInfo source = event.getSource();        
    if(event.getEventType()==AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) 
        explore(source);

}

private void explore(AccessibilityNodeInfo view){
    int count = view.getChildCount();
    for(int i=0; i<count; i++){
        AccessibilityNodeInfo child = view.getChild(i);
        if(!MODE_TALK_BACK_SCREEN){
            if(child.getText()!=null && child.getText().toString().toLowerCase().contains("TalkBack")){
                child.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                MODE_TALK_BACK_SCREEN=true;
                return;
            }
        }else{
            if("ToggleButton".equals(child.getClassName().toString())){ //there ony one toggle button on the screen
                child.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                performGlobalAction(GLOBAL_ACTION_BACK);
                performGlobalAction(GLOBAL_ACTION_BACK);//need to go back two time - i don't know if that will work :)
                return;
            }
        }
        explore(child);
        child.recycle();
    }

所以现在,如果您使用 Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS); 打开辅助功能设置,它将为您执行点击 - 您将不得不以某种方式用全屏 toast 或视图

服务覆盖它

我目前正在研究自动飞行模式切换并且它有效 - 所以应该在你的情况下完成这项工作

看看我的serviceconfig.xml

<accessibility-service     xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/accessibility_service_description"
android:packageNames="com.android.settings"
android:accessibilityEventTypes="typeWindowStateChanged"
android:accessibilityFlags="flagDefault"
android:accessibilityFeedbackType="feedbackSpoken"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity"
/>