Android开屏尺寸去precated? [英] Android get Screen Size deprecated?

查看:342
本文介绍了Android开屏尺寸去precated?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我需要得到屏幕的宽度,在我的应用程序。该应用程序将运行在2.1及以上。我已经将它设置了一个类似下面的。该方法是去precated,我应该proabably使用的getSize或其他方式。但问题是:将这项工作在Android版本,像3.0+和4.0+,还是会令应用程序崩溃。我已经使用了德precated方法之前,一个线程,它让冰淇淋的设备的应用程序崩溃。下面的工作方法?

 显示显示= getWindowManager()getDefaultDisplay()。
     INT宽度= display.getWidth();
     INT高= display.getHeight();
 

编辑:

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

 显示显示= getWindowManager()getDefaultDisplay()。
      点大小=新的点();
      display.getSize(大小);
      INT宽度= size.x;
      INT高= size.y;
 

解决方案

我不知道这些去precated方法是否能够在Android 3和4。告诉是测试模拟器上的最好办法。

不过,最安全的方法,这里最大兼容性会使用反射来尝试一种方法,并回落到其他。从本质上讲,你可以使你自己版本的的getSize()不能失败。我不能对此进行测试大气压,但它可能是这样的:

 无效overrideGetSize(陈列展示,点如雷贯耳){
    尝试 {
      //测试新的方法来触发异常
      类pointClass =的Class.forName(android.graphics.Point);
      方法newGetSize = Display.class.getMethod(的getSize,新的等级[] {pointClass});

      //也不例外,所以新的方法是可用的,只是用它
      newGetSize.invoke(显示器,如雷贯耳的);
    }赶上(NoSuchMethodException前){
      //新的方法不可用,用旧的
      outSize.x = display.getWidth();
      outSize.y = display.getHeight();
    }
}
 

那当然只是把它的东西,如

 显示显示= getWindowManager()getDefaultDisplay()。
点大小=新的点();
overrideGetSize(显示器,尺寸);
 

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;

解决方案

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

这篇关于Android开屏尺寸去precated?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆