문제

I saw in a android video from google developers that they implemented two methods and defined via annotation what method should be called based on the api level.

Unfortunatelly, I can`t find the video anymore(I can't remember which video was), so I searched StackOverflow for this question and found this: Different Java methods for different API Levels

In the question he used the annotation @apilevel and I can't find that annotation.

Basically, what I want to do is this:

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private int getDisplayWidth(Display display){
    Point size = new Point();
    display.getSize(size);
    return size.x;
}

@TargetApi(Build.VERSION_CODES.FROYO)
private int getDisplayWidth(Display display){
    return display.getWidth();
}

But ADT is giving me duplicated method error.

도움이 되었습니까?

해결책

Use:

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private int getDisplayWidth(Display display){
    if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB_MR2) {
      Point size = new Point();
      display.getSize(size);
      return size.x;
    }

    return display.getWidth();
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top