1

I am trying to incorporate a try/catch in order to support a deprecated method.

If my minSdkVersion is too low to support the (newer) method that I am trying, Eclipse sees an error because it isn't supported (yet). How do I get Eclipse to ignore this line?

Here's the code:

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();

    try { 
        display.getSize(size); 
        width = size.x; 
        height = size.y; 
    } catch (NoSuchMethodError e) {
        width = display.getWidth();
        height = display.getHeight(); 
    } 

With a minsdkversion before 14, eclipse doesn't like the Display.getSize(size).

Alternatively, is there a better way to do this?

thanks

Jon Biz
  • 993
  • 2
  • 8
  • 23

3 Answers3

1

This is a place where reflection is useful: try to get the getSize method from the class. If you successfully get it, use it, otherwise fallback to the old style.

parsifal
  • 301
  • 1
  • 2
  • I use reflection for this purpose all the time but for some reason, SuppressLint was the first thing I thought of. I'm getting rusty or something. – Michael Celey Sep 27 '12 at 20:32
  • @MCeley - I wasn't aware that Eclipse (or the Android SDK) had that functionality, and to be honest, it scares me even though I think it's very useful for a platform that evolves as quickly as Android. – parsifal Sep 27 '12 at 20:40
1

I believe this is relevant: How to retrieve the android sdk version?

Build.VERSION.SDK_INT is the one you need. Simple if/else to replace the try/catch.

Community
  • 1
  • 1
ECrownofFire
  • 462
  • 4
  • 11
0

If you bump your build target up to Jelly Bean then you can use @SuppressLint("NewApi") to ignore the error. I don't think there's another way to ignore the error with the new Android tools.

EDIT: Actually, you don't need to bump your build target up to Jelly Bean. I have SuppressLint working in 4.0.3 currently. You just need the Android annotations JAR that comes with the SDK.

Michael Celey
  • 12,087
  • 6
  • 53
  • 60