How to check if Talkback is active in JellyBean

后端 未结 4 1371
半阙折子戏
半阙折子戏 2021-01-06 08:49

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 curs

4条回答
  •  Happy的楠姐
    2021-01-06 09:16

    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 screenReaders = getPackageManager().queryIntentServices(screenReaderIntent, 0);
    Cursor cursor = null;
    int status = 0;
    ContentResolver cr = getContentResolver();
    
    List runningServices = new ArrayList();
    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
            }
        }
    }
    

    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

提交回复
热议问题