简体   繁体   中英

Android Call requires API level 13 (current min is 10): android.view.Display#getSize

I'm trying to target as many architectures as possible, as well as having few warnings as possible.

Point dims = new Point();
if (android.os.Build.VERSION.SDK_INT >= 13) {
      mWindowManager.getDefaultDisplay().getSize(dims);
} else if (android.os.Build.VERSION.SDK_INT < 13) {
  dims.x = mWindowManager.getDefaultDisplay().getWidth();
  dims.y = mWindowManager.getDefaultDisplay().getHeight();
}

But this gives me the error and warning:

Call requires API level 13 (current min is 10): android.view.Display#getSize
The method getWidth() from the type Display is deprecated

They say to change the manifesto (or here ) but why is this above not working for the compiler? Can't I get rid of both with the range of apis 10 to 18 ?

You already have the SDK version checking in place so the deprecation warning from compiler and lint warning about new API can be ignored.

Pull the code snippet to a separate method and add the following annotations to it:

@TargetApi(13)
@SuppressWarnings("deprecation")

Note that the if part of your else if is redundant. Plain else is enough.

To retrieve the display size, the recommended way is to use

context.getResources().getDisplayMetrics()

instead. Then use the widthPixels and heightPixels properties.

Using the current context gives you the dimensions of the current display in case of multiple displays, which is also safer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM