开发者

Android - Override method from higher API version and supporting lower API version

I'd like to listen for a long key press in my Android application, and from Android 2.0 there is a method

public boolean onKeyLongPress(int keyCode, KeyEvent event)

to override. But what can I do if my app absolutely has to support API 4 (Android 1.6)? I know that I can call API methods with refle开发者_Python百科ction, but I'm pretty sure that I cannot override with reflection.


Why don't you just remove @Override annotation above the method? Android 1.6 would ignore it, 2.0 would still interpret it correctly.


The easiest is to write two implementations of your custom view class, say:

MyCustomViewBasic extends View {
    private MySharedImplementation impl;
}

MyCustomViewKeyLongPress extends View {
    private MySharedImplementation impl;

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

These two implementations can share as many implementation details as possible, whilst ensuring that anything not available in API level 4 is not in the shared implementation.

Then have two xml layouts, one for API level 4 and one for API level 4 and above. Use MyCustomViewBasic in the layout for API level 4 and MyCustomViewKeyLongPress in the one for API level 4 and above

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜