6

I'm trying to create my own ImagePreference that I will use in my app preferences in order to pick the picture the user chooses to represent himself.

I'm not too sure about what I'm doing, but basically I'm mimicking some other preference classes. So far I'm only having trouble with this method:

public class ImagePreference extends Preference {
...
    void sendAccessibilityEvent(View view) {
        // Since the view is still not attached we create, populate,
        // and send the event directly since we do not know when it
        // will be attached and posting commands is not as clean.
        AccessibilityManager accessibilityManager = AccessibilityManager.getInstance(getContext());
        if (mSendClickAccessibilityEvent && accessibilityManager.isEnabled()) {
            AccessibilityEvent event = AccessibilityEvent.obtain();
            event.setEventType(AccessibilityEvent.TYPE_VIEW_CLICKED);
            view.onInitializeAccessibilityEvent(event);
            view.dispatchPopulateAccessibilityEvent(event);
            accessibilityManager.sendAccessibilityEvent(event);
        }
        mSendClickAccessibilityEvent = false;
    }

I have just copied this code from the TwoStatePreference class. I'm not even sure I need this method at all, but the compiler complains saying that the AccessibilityManager class does not contain a getInstance method.

I thought this could be a problem with the imports, but I'm importing the same classes as TwoStatePreference does, i.e.:

import android.view.accessibility.AccessibilityManager;

I have lost already several hours with this problem. Any idea of why the compiler is complaining?

Thanks!

ivansiiito
  • 133
  • 2
  • 10

2 Answers2

9

Indeed, the AccessibilityManager class has no method called getInstance(). From the docs:

To obtain a handle to the accessibility manager do the following:

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

So really all you need is a Context. Lucky, you've got a View, which itself is a Context, and holds references to it's parent contexts.

Collin Flynn
  • 751
  • 6
  • 8
  • Perfect! Thanks a lot (:-)= However, I don't get why it didn't work in my way, since I got that code from the android src, and I can see this method in the AccessibilityManager class: public static AccessibilityManager getInstance(Context context) { return sInstance; }. May I have some problem with my sources? It seems everything is up to date. Thanks again! – ivansiiito Aug 08 '13 at 21:20
  • Yes i was looking at some components developed by google they have used the AccessibilityManager.getInstance(this.getContext()) when i tried the same in my code it doesnt work – Dominic D'Souza Jun 11 '15 at 04:15
  • Also opening the AccessibilityManager class from android studio showed me this /** * Get an AccessibilityManager instance (create one if necessary). * */ public static AccessibilityManager getInstance(Context context) { return sInstance; } – Dominic D'Souza Jun 11 '15 at 04:17
7

This method had been hidden. The source code:

 /**
 * Get an AccessibilityManager instance (create one if necessary).
 *
 * @param context Context in which this manager operates.
 *
 * @hide
 */
public static AccessibilityManager getInstance(Context context)
EM-Creations
  • 4,195
  • 4
  • 40
  • 56
Ado
  • 411
  • 4
  • 9