且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 JellyBean 中检查 Talkback 是否处于活动状态

更新时间:2023-11-27 23:52:10

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

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