如何在 Android 中以编程方式启用/禁用辅助功能服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10061154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:21:05  来源:igfitidea点击:

How to Programmatically Enable/Disable Accessibility Service in Android

androidserviceaccessibility

提问by Abilash

I would like to programmatically enable/disable Accessibility Services listed under Settings->Accessibility option.

我想以编程方式启用/禁用“设置”->“辅助功能”选项下列出的辅助功能服务。

I could start Accessibility Intent like below:

我可以像下面这样启动可访问性意图:

Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivityForResult(intent, 0);

But I don't have any idea on how to enable the services listed in the view through my code.

但是我不知道如何通过我的代码启用视图中列出的服务。

Please, provide me your views.

请给我你的意见。

回答by Kevin

I found a solution worked for me by calling

我通过致电找到了对我有用的解决方案

Settings.Secure.putString(getContentResolver(), 
    Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, "pkgname/classname");
Settings.Secure.putString(getContentResolver(), 
    Settings.Secure.ACCESSIBILITY_ENABLED, "1");

Where the pkgnameis your package name and the classnameis the class name of your accessibility service.

其中pkgname是您的包名,classname是您的无障碍服务的类名。

If you need to enable several services or you don't want to destory the previous settings you might want to use :to seperate other services.

如果您需要启用多个服务,或者您不想破坏以前的设置,您可能想要:用来分离其他服务。

Also you might need to run as a system applicationand you might need the following permissions

此外,您可能需要作为系统应用程序运行,并且您可能需要以下权限

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

However, according to @Rupert Rawnsleythis might not work on some versions of Android, I am working on Android 4.0.4 and hope it works for you.

但是,根据@Rupert Rawnsley 的说法,这可能不适用于某些版本的 Android,我正在使用 Android 4.0.4,希望它对您有用

PS. If it doesn't work, maybe you could find some luck in /data/data/com.android.providers.settings/databases/settings.db/secure, that's where Android stores secure settings.

附注。如果它不起作用,也许您可​​以在 中找到一些运气/data/data/com.android.providers.settings/databases/settings.db/secure,那就是 Android 存储安全设置的地方。

回答by Zoli_K

From Android 6.0 you can use:

从 Android 6.0 开始,您可以使用:

adb shell settings put secure enabled_accessibility_services packagname/servicename

The settings.db from old releases is no longer present on Android 6.0.

旧版本的 settings.db 不再存在于 Android 6.0 上。

回答by Sam

In Android 7 (API 24), an AccessibilityService can programmatically disable itself by calling the disableSelf()method.

在 Android 7 (API 24) 中,AccessibilityService 可以通过调用disableSelf()方法以编程方式禁用自身。

回答by Rupert Rawnsley

AccessibilityService is special and cannot be started programmatically.

AccessibilityService 是特殊的,不能以编程方式启动。

回答by Eran Katsav

The best you can do is manualy open the accessibilty settings with:

您能做的最好的事情是手动打开辅助功能设置:

Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)

意图意图 = 新意图(Settings.ACTION_ACCESSIBILITY_SETTINGS)

and start the intent - you can also do it from the prefernece xml file:

并启动意图 - 您也可以从首选项 xml 文件中执行此操作:

intent android:action="android.settings.ACCESSIBILITY_SETTINGS"

意图 android:action="android.settings.ACCESSIBILITY_SETTINGS"

回答by adb_user

Just in case anybody still seeks to turn offtalkback from adb when you are stuck in your lock screen entering the password or pin. One thing you can try is adb shell am force-stop com.google.android.marvin.talkback

以防万一当您卡在锁屏界面输入密码或 PIN 码时,仍然有人试图关闭来自 adb 的对讲。你可以尝试的一件事是adb shell am force-stop com.google.android.marvin.talkback

回答by Maxim Blumental

In instrumented tests I start my accessibility service like this:

在仪器测试中,我像这样启动我的无障碍服务:

private fun enableAccessibilityService() {
    val packageName = "com.example"
    val className = "$packageName.service.MyService"
    val string = "enabled_accessibility_services"
    val cmd = "settings put secure $string $packageName/$className"
    InstrumentationRegistry.getInstrumentation()
      .getUiAutomation(UiAutomation.FLAG_DONT_SUPPRESS_ACCESSIBILITY_SERVICES)
      .executeShellCommand(cmd)
      .close()
    TimeUnit.SECONDS.sleep(3)
}

I tested on Android 6 and 8. This also works for non-system apps.

我在 Android 6 和 8 上进行了测试。这也适用于非系统应用程序。

回答by BlackShaman

Here is the working solution (if your activity is not running it will disable itself)

这是有效的解决方案(如果您的活动未运行,它将自行禁用)

@Override
protected void onServiceConnected() {
    super.onServiceConnected();
    Toast.makeText(this, "Connected!",Toast.LENGTH_SHORT).show();
    ScheduledExecutorService scheduleTaskExecutor = Executors.newScheduledThreadPool(1);
    scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
        public void run() {
            serviceChecker();
        }
    }, 0, 5, TimeUnit.SECONDS);
}

private void serviceChecker(){
    if(!isActivityRunning(MainActivity.class)) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            disableSelf();
        }
    }
}
protected Boolean isActivityRunning(Class activityClass)
{
    ActivityManager activityManager = (ActivityManager) getBaseContext().getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);

    for (ActivityManager.RunningTaskInfo task : tasks) {
        if (activityClass.getCanonicalName().equalsIgnoreCase(task.baseActivity.getClassName()))
            return true;
    }

    return false;
}

回答by Ryan B

I found this post: How to programmatically check if a service is declared in AndroidManifest.xml?. The top answer talks about PackageManager, which tells you what is running.

我找到了这篇文章:如何以编程方式检查服务是否在 AndroidManifest.xml 中声明?. 最重要的答案是关于 PackageManager,它告诉你正在运行什么。