是否可以安全使用.getWidth上显示,即使其德precated使其、安全、getWidth、precated

2023-09-04 11:03:21 作者:子当归

所以我有一个小问题,我正在写这需要屏幕宽度发送到服务器的功能。我得到了这一切的工作,我用:

So i have a small problem, i'm writing a function which need to send screen width to server. I got it all to work, and i use:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();

获得宽度。然而.getWidht()函数去precated,它说你需要使用:

to get width. However .getWidht() function is deprecated and it says u need to use:

Point size = new Point();
display.getSize(size);

不过,该功能仅avaible的API级别13以上,我最小的SDK是8。所以,我该怎么办?它是安全的,如果我留在的getWidth?为什么要增加新的功能,而不是让他们向后兼容?

But that function is only avaible for api level 13 or more, and my minimum sdk is 8. So what can i do? Is it safe if i stay with getWidth? Why adding new function and not make them backward compatible?

推荐答案

可能是这种方式会有所帮助:

May be this approach will be helpful:

DisplayMetrics displaymetrics = new DisplayMetrics();
mContext.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screenWidth = displaymetrics.widthPixels;
int screenHeight = displaymetrics.heightPixels;