Android's InputManager::registerInputDeviceListener isn't calling my listener

4

I'm trying to get a notification when input devices are added/removed, and from what I understand that's what registerInputDeviceListener should do... but my listener isn't being called!

Here's a snippet of my code:

InputManager im = (InputManager) getSystemService(Context.INPUT_SERVICE);
im.registerInputDeviceListener(new InputManager.InputDeviceListener() {
    @Override
    public void onInputDeviceAdded(int deviceId) {
        Log.d("Input", "InputDeviceAdded: " + deviceId);
    }

    @Override
    public void onInputDeviceRemoved(int deviceId) {
            Log.d("Input", "InputDeviceRemoved: " + deviceId);
    }

    @Override
    public void onInputDeviceChanged(int deviceId) {
        Log.d("Input", "InputDeviceChanged: " + deviceId);
    }
}, null);

And here's what I see in logcat when I unplug my usb mouse:

01-15 19:19:04.025: INFO/EventHub(5935): Removing device '/dev/input/event0' due to inotify event
01-15 19:19:04.025: INFO/EventHub(5935): Removed device: path=/dev/input/event0 name=Primax USB OPTICAL MOUSE id=11 fd=245 classes=0x80000008
01-15 19:19:04.045: INFO/InputReader(5935): Device removed: id=11, name='Primax USB OPTICAL MOUSE', sources=0x00002002

But my listener never gets called...

java
android
asked on Stack Overflow Jan 15, 2013 by Buddy

1 Answer

4

Turns out that the InputManager doesn't register itself for device changes unless getInputDevice or getInputDeviceByDescriptor is called previously.

Calling getInputDevice first (and ignoring the result) makes my callbacks get called.

answered on Stack Overflow Jan 17, 2013 by Buddy

User contributions licensed under CC BY-SA 3.0