2012-05-18 75 views
14

嘿,我需要在我的应用程序中获得屏幕的宽度。该应用程序将运行在2.1及更高版本上。我已经将它设置为如下所示。该方法已被弃用,我应该proabably使用getSize或其他方式。但问题是:这会在3.0+和4.0+等Android版本上运行,还是会导致应用程序崩溃。我之前在一个线程中使用了一个不推荐使用的方法,它使应用程序在冰淇淋设备上崩溃。下面的方法会工作吗?Android获取屏幕尺寸已弃用?

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

编辑:

我已经试过的getSize,但我不得到它的工作:

Display display = getWindowManager().getDefaultDisplay(); 
     Point size = new Point(); 
     display.getSize(size); 
     int width = size.x; 
     int height = size.y; 

回答

12

我不知道这些弃用的方法是否可以在Android 3和4上工作。最好告诉的方法是在模拟器上测试。

但是,这里最安全的方法最大兼容性将尝试一种方法使用反射,并回落到另一个。本质上,你可以制作你自己的版本getSize(),它不会失败。我不能对此进行测试ATM,但它可能是这样的:

void overrideGetSize(Display display, Point outSize) { 
    try { 
     // test for new method to trigger exception 
     Class pointClass = Class.forName("android.graphics.Point"); 
     Method newGetSize = Display.class.getMethod("getSize", new Class[]{ pointClass }); 

     // no exception, so new method is available, just use it 
     newGetSize.invoke(display, outSize); 
    } catch(NoSuchMethodException ex) { 
     // new method is not available, use the old ones 
     outSize.x = display.getWidth(); 
     outSize.y = display.getHeight(); 
    } 
} 

那当然只是像

Display display = getWindowManager().getDefaultDisplay(); 
Point size = new Point(); 
overrideGetSize(display, size); 
+0

你能解释一下新的Class [] {pointClass}的含义吗? – opc0de

+0

为了通过反射来获得一个方法,你必须传入一个参数类的数组。如果方法过载,则必须指定参数。查看['getMethod()']的文档(https://developer.android.com/reference/java/lang/Class.html)。所以我们为'Point'类创建了一个数组,因为'getSize()'只有这个参数。这有帮助吗? –

+0

谢谢我对java世界很新颖 – opc0de

0

有什么不对显示器的新功能,getSize()?将Point对象转换为所需的宽度和高度值非常简单。

+2

心不是所述的getSize()方法从仅API缴费拉特13和向上还是我误? – Ukjent

23

称它为我不知道,但是这可能工作:

if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2) { 
    Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth(); 
    int height = display.getHeight(); 
} else { 
    Display display = getWindowManager().getDefaultDisplay(); 
    Point size = new Point(); 
    display.getSize(size); 
    int width = size.x; 
    int height = size.y; 
} 
+0

getHeight()现已被弃用... –

3

我已经扩展了Steve的有用代码,以便Eclipse不会给出任何警告或错误,并且我还稍微重构了它。由于API级别从API级别1开始出现,所以我没有看到通过反射创建它的很多好处。

final static String mTAG = "MYTAG"; 

    // Cope with deprecated getWidth() and getHeight() methods 
    Point getSize(Display xiDisplay) 
    { 
    Point outSize = new Point(); 
    boolean sizeFound = false; 

    try 
    { 
     // Test if the new getSize() method is available 
     Method newGetSize = 
     Display.class.getMethod("getSize", new Class[] { Point.class }); 

     // No exception, so the new method is available 
     Log.d(mTAG, "Use getSize to find screen size"); 
     newGetSize.invoke(xiDisplay, outSize); 
     sizeFound = true; 
     Log.d(mTAG, "Screen size is " + outSize.x + " x " + outSize.y); 
    } 
    catch (NoSuchMethodException ex) 
    { 
     // This is the failure I expect when the deprecated APIs are not available 
     Log.d(mTAG, "getSize not available - NoSuchMethodException"); 
    } 
    catch (InvocationTargetException e) 
    { 
     Log.w(mTAG, "getSize not available - InvocationTargetException"); 
    } 
    catch (IllegalArgumentException e) 
    { 
     Log.w(mTAG, "getSize not available - IllegalArgumentException"); 
    } 
    catch (IllegalAccessException e) 
    { 
     Log.w(mTAG, "getSize not available - IllegalAccessException"); 
    } 

    if (!sizeFound) 
    { 
     Log.i(mTAG, "Used deprecated methods as getSize not available"); 
     outSize = new Point(xiDisplay.getWidth(), xiDisplay.getHeight()); 
    } 

    return outSize; 
    }