[英]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 ?
难道我不能摆脱10到18的api范围吗?
You already have the SDK version checking in place so the deprecation warning from compiler and lint warning about new API can be ignored. 您已经对SDK版本进行了检查,因此可以忽略编译器的弃用警告和有关新API的lint警告。
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. 需要注意的是
if
你的一部分, else if
是多余的。 Plain else
is enough. 简单的
else
就够了。
To retrieve the display size, the recommended way is to use 要检索显示大小,建议使用的方法
context.getResources().getDisplayMetrics()
instead. 代替。 Then use the widthPixels and heightPixels properties.
然后使用widthPixels和heightPixels属性。
Using the current context gives you the dimensions of the current display in case of multiple displays, which is also safer. 使用当前上下文为您提供多个显示器当前显示的尺寸,这也更安全。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.