我试图在添加/删除输入设备时收到通知,据我所知,这就是 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);
这是我拔下 USB 鼠标时在 logcat 中看到的内容:
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
但我的听众永远不会被叫到......