2013-01-15 51 views
2

我試圖得到一個通知,當輸入設備被添加/刪除,並從我理解這是什麼registerInputDeviceListener應該做的......但我的聽衆沒有叫!Android的InputManager :: registerInputDeviceListener是不是打電話給我的聽衆

這裏是我的代碼片段:

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); 

這裏就是我在logcat中看到當我拔掉我的USB鼠標:

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 

但我的聽衆不會被調用...

回答

3

原來,除非先前調用getInputDevice或getInputDeviceByDescriptor,否則表明InputManager不會爲設備更改註冊自身。

首先調用getInputDevice(並忽略結果)會調用我的回調函數。