【问题标题】:Android - InputManager.InputDeviceListener NewApi exceptionAndroid - InputManager.InputDeviceListener NewApi 异常
【发布时间】:2014-04-15 08:24:06
【问题描述】:

我想从连接到我的 Android 的操纵杆中捕捉按键事件和运动事件。

我正在使用 InputManager.InputDeviceListener,但支持的最低 API 级别是 14,InputManager.InputDeviceListener 需要 API 级别 16。

您知道 API 级别 14 支持的解决方案吗?

没有编译的代码:

public class MyActivity extends Activity implements InputManager.InputDeviceListener {

    @Override
    public void onResume() {        
        inputManager.registerInputDeviceListener(this, null);

        // Query all input devices.
        // We do this so that we can see them in the log as they are enumerated.
        int[] ids = inputManager.getInputDeviceIds();
        for (int i = 0; i < ids.length; i++) 
        {
            getInputDeviceState(ids[i]);
        }
    }


    @Override
    protected void onPause()
    {
        inputManager.unregisterInputDeviceListener(this);
    }

    public InputDeviceState getInputDeviceState(int deviceId) 
    {
        InputDeviceState state = inputDeviceStates.get(deviceId);
        if (state == null) 
        {
            final InputDevice device = inputManager.getInputDevice(deviceId);
            if (device == null) 
            {
                return null;
            }
            state = new InputDeviceState(device);
            inputDeviceStates.put(deviceId, state);

        }
        return state;
    }

}

代码来自 Android 示例。

【问题讨论】:

    标签: android joystick


    【解决方案1】:

    只看你的代码...

    您将获得例外,因为您遗漏了super.onResume();super.onPause();

    【讨论】:

      【解决方案2】:

      您可以在此处找到有关如何跨 Android Android 版本支持输入设备侦听器的示例代码: http://developer.android.com/training/game-controllers/compatibility.html#newer

      本质上,解决方案是为 InputManager 和 InputDeviceListener 提供新的类和接口,在 API 级别 16 和更高级别上实现为 SDK 类的包装器,在 API 级别 9 到 15 上它们实现跟踪和通知连接的输入设备。在运行时,应用应通过比较 Build.VERSION.SDK_INT 和 Build.VERSION_CODES.JELLY_BEAN 来选择适当的实现。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-06-20
        • 2012-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多