Android get Screen Size deprecated?

主宰稳场 提交于 2020-01-01 02:01:14

问题


Hey I need to get the width of the screen in my application. The application will run on 2.1 and upwards. I have set it up like the one below. The method is deprecated and i should proabably use getSize or a other way. But the question is: Will this work on android versions like 3.0+ and 4.0+, or will it make the app crash. I have used a deprecated method in a thread before and it made the app crash on ice cream devices. Will the method below work ?

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

EDIT:

I have tried the getSize but i dont get it to work:

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

回答1:


I don't know whether these deprecated methods will work on Android 3 and 4. The best way to tell is to test on an emulator.

But, the safest method here for max compatibility will be to try one method using reflection, and fall back to the other. Essentially, you could make your own version of getSize() that can't fail. I can't test this atm, but it might look like this:

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();
    }
}

Then of course just call it with something like

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



回答2:


I am not sure but this may work:

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;
}



回答3:


I've extended Steve's helpful code so that Eclipse doesn't give any warnings or errors, and I've also restructured it slightly. Since the Point class has been present since API level 1 I didn't see much benefit in creating it through reflection.

  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;
  }



回答4:


According to https://stackoverflow.com/a/1016941/2914140:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;



回答5:


What is wrong with Display's new function, getSize()? It'd be really easy to turn the Point object into the width and height values you need.



来源:https://stackoverflow.com/questions/10659954/android-get-screen-size-deprecated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!