Android:如何找到屏幕的宽度和高度?

我试图找到屏幕的宽度和高度,但我尝试的方法都没有在我创建的类中工作,我的代码如下。

有谁知道如何找到它? 我不能使用我在下面尝试的方式,因为.getWidth()已被弃用?

public class Crate { public int acrossCrate; public int upDownCrate; Context theContext; WindowManager wm = (WindowManager)theContext.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); int width = display.getWidth(); public Crate() { acrossCrate = 650; upDownCrate = 650; //int width = ; //int height = ; } } 

使用下面的代码

 private int getScreenHeight(Context context) { int height; if (android.os.Build.VERSION.SDK_INT >= 13) { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); Point size = new Point(); display.getSize(size); height = size.y; } else { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); height = display.getHeight(); // deprecated } return height; }