Polam Naganji
Polam Naganji

Reputation: 287

How to detect when user turns on/off talkback in android programmatically

I'm trying to update the UI based on accessibility option (i.e. talkback on/off). When user turns on talkback, i need to show some helper text else the helper text should be hidden.

As we also have shortcuts (In Pixel device, long pressing on both up and down volume keys) to enable/disable talkback. Is there any way to detect whether talkback is enabled/disabled, when i am on XYZ activity.

Upvotes: 3

Views: 4047

Answers (1)

May Rest in Peace
May Rest in Peace

Reputation: 2197

You can use AccessibilityManager.AccessibilityStateChangeListener to receive whether listen to changes in Accessibilty Service.

Unfortunately this will be true if ANY of the accessibility services are enabled.

AccessibilityManager accessibilityManager= (AccessibilityManager) getSystemService(Context.ACCESSIBILITY_SERVICE);

// Put this in onCreate
accessibilityManager.addAccessibilityStateChangeListener(new AccessibilityManager.AccessibilityStateChangeListener() {
    @Override
    public void onAccessibilityStateChanged(boolean b) {
        accessibilityChanged(b);
    }
});
accessibiltyChanged(accessibiltyManager.isEnabled());

void accessibiltyChanged (Boolean enabled) {
   // Do your stuff
}

You can also try using getEnabledAccessibilityServiceList. This will return the list of all active accessibility services. But this will not be a listener meaning you'll not get a callback if this changes. A hack would be to call this function at regular intervals to check if anything has changed.

Upvotes: 2

Related Questions