如何在 JellyBean 中检查 Talkback 是否处于活动状态 [英] How to check if Talkback is active in JellyBean

查看:28
本文介绍了如何在 JellyBean 中检查 Talkback 是否处于活动状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题询问如何知道Android Talkback活跃;一直工作到果冻豆.从 Android 4.1 开始,这些步骤不再有效,因为提到的光标是空的.

This question asked how to know if Android Talkback is active; that worked until Jelly Bean. Starting from Android 4.1, that steps no longer work, because the mentioned cursor is empty.

说到这里,我想问的是是否有办法在 Jelly Bean 中进行同样的检查.

Having this said, I want to ask is if there is a way to do the same checking in Jelly Bean.

编辑我试图搜索 TalkBack 代码,我找到了它 此处.为了检查 TalkBack 是否处于活动状态,我使用以下代码:

EDIT I tried to search for TalkBack code and I found it here. For checking if TalkBack is active, I am using the following code:

Intent screenReaderIntent = new Intent("android.accessibilityservice.AccessibilityService");
screenReaderIntent.addCategory("android.accessibilityservice.category.FEEDBACK_SPOKEN");
List<ResolveInfo> screenReaders = getPackageManager().queryIntentServices(screenReaderIntent, 0);
Cursor cursor = null;
ContentResolver cr = getContentResolver();
for (ResolveInfo screenReader : screenReaders) {
    cursor = cr.query(Uri.parse("content://" + screenReader.serviceInfo.packageName
            + ".providers.StatusProvider"), null, null, null, null);
    //here, cursor is not null, but calling cursor.moveToFirst() returns false, which means the cursor is empty
}

话虽如此,如果光标为空,我们如何知道 TalkBack 是否正在运行?

Having this said, if the cursor is empty, how do we know if TalkBack is running?

编辑 2按照@JoxTraex 的建议,我现在正在发送广播以查询是否启用了 TalkBack:

EDIT 2 Following @JoxTraex suggestions, I am now sending a broadcast to query whether or not TalkBack is enabled:

Intent i = new Intent();
i.setAction("com.google.android.marvin.talkback.ACTION_QUERY_TALKBACK_ENABLED_COMMAND");
sendBroadcast(i);

现在我应该如何收到回复?

Now how should I receive the response?

我尝试将以下内容添加到清单中,但我的接收器没有收到任何响应:

I tried adding the following to manifest, but my receiver does not receive any response:

<receiver android:name="my.package.MyBroadcastReceiver"
android:permission="com.google.android.marvin.talkback.PERMISSION_SEND_INTENT_BROADCAST_COMMANDS_TO_TALKBACK">
<intent-filter>
    <action android:name="com.google.android.marvin.talkback.ACTION_QUERY_TALKBACK_ENABLED_COMMAND" />
</intent-filter>

推荐答案

我不确定这是实现建议的最佳方式,但我使用以下代码设法使这项工作:

I'm not sure this is the best way of achieving what is proposed, but I managed to make this work by using the following code:

Intent screenReaderIntent = new Intent("android.accessibilityservice.AccessibilityService");
screenReaderIntent.addCategory("android.accessibilityservice.category.FEEDBACK_SPOKEN");
List<ResolveInfo> screenReaders = getPackageManager().queryIntentServices(screenReaderIntent, 0);
Cursor cursor = null;
int status = 0;
ContentResolver cr = getContentResolver();

List<String> runningServices = new ArrayList<String>();
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
    runningServices.add(service.service.getPackageName());
}

for (ResolveInfo screenReader : screenReaders) {
    cursor = cr.query(Uri.parse("content://" + screenReader.serviceInfo.packageName
            + ".providers.StatusProvider"), null, null, null, null);

    if (cursor != null && cursor.moveToFirst()) { //this part works for Android <4.1
        status = cursor.getInt(0);
        cursor.close();
        if (status == 1) {
            //screen reader active!
        } else {
            //screen reader inactive
        }
    } else {  //this part works for Android 4.1+
        if (runningServices.contains(screenReader.serviceInfo.packageName)) {
            //screen reader active!
        } else {
            //screen reader inactive
        }
    }
}

可能这不是最好的方法,但它是我能想到的唯一一种适用于 Jelly Bean 和以前的 Android 版本的方法

Probably this is not the best way but it is the only one I can think of that works in Jelly Bean and in previous Android versions

这篇关于如何在 JellyBean 中检查 Talkback 是否处于活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆