0

I want to catch events from keys and motion events from joystick connected to my Android.

I'm using InputManager.InputDeviceListener but the minimal API level supported is 14 and InputManager.InputDeviceListener requires API level 16.

Do you know a solution supported by API level 14 ?

Code that not compiling :

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

}

Code is from Android samples.

Florian Mac Langlade
  • 1,863
  • 7
  • 28
  • 57

2 Answers2

0

Just looking at your code ...

You will get Exceptions because you left out super.onResume(); and super.onPause();

enrico.bacis
  • 30,497
  • 10
  • 86
  • 115
gabriel
  • 1,163
  • 2
  • 9
  • 15
0

You can find sample code about how to support input device listeners across Android Android Versions here: http://developer.android.com/training/game-controllers/compatibility.html#newer

In essence the solution is to provide new class and interface for the InputManager and the InputDeviceListener, which on API level 16 and higher are implemented as wrappers of the SDK classes, and on API level 9 to 15 they implement tracking and notifications for the connected input devices. At runtime the app should select the appropriate implementation by comparing Build.VERSION.SDK_INT to Build.VERSION_CODES.JELLY_BEAN.

Maria
  • 633
  • 5
  • 12