Android - override method from a higher version of the API and support for a lower version of the API

I would like to listen to the long press in an Android app, but with Android 2.0 there is a method

public boolean onKeyLongPress(int keyCode, KeyEvent event)

for redefinition. But what can I do if the application really supports API 4 (Android 1.6)? I know that I can call API methods with reflection, but I'm sure I can not override with reflection.

+5
source share
2 answers

Why don't you just remove the annotation @Overrideover the method? Android 1.6 ignores it, 2.0 will still interpret it correctly.

+5
source

The easiest way is to write two implementations of your custom view class, for example:

MyCustomViewBasic extends View {
    private MySharedImplementation impl;
}

MyCustomViewKeyLongPress extends View {
    private MySharedImplementation impl;

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        // Do something exciting
    }
}

, , , API 4, .

xml-, API 4 API 4 . MyCustomViewBasic API 4 MyCustomViewKeyLongPress API 4

0

All Articles