是否确定在向后兼容code如此多的去precated方法? [英] Is it OK to have so many deprecated methods in backward-compatible code?

查看:231
本文介绍了是否确定在向后兼容code如此多的去precated方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写这针对的是API 15级的Andr​​oid应用程序,但我也想保持向后相容性与旧的API级别(分-SDK 7)。

I'm writing an Android application which is targeted to API level 15 but I also want to keep backward-compatibilty with older API levels (min-sdk 7).

我将通过把条件决定哪些code根据当前API级别使用(如下图所示),以达到这一点。我想这是一个好办法,我只想问是否确定有这么多的去precated方法(如 display.getWidth()),因为有是API级别8和15之间相当多的变化。 如果是的话,如果是在这种情况下,一个良好的使用 @燮pressWarning(德precation)吗?

I'm going to reach this by putting conditions deciding which code to use according to current API level (as shown below). I suppose this is a good approach, I just want to ask if it's OK to have so many deprecated methods (like display.getWidth()), because there is quite many changes between API level 8 and 15. And if so, if it's a good use of @SuppressWarning("deprecation") in this case?

那岂不是更好地使用多个APK例如用于API等级< = 8,> = 9? (虽然它不建议 developer.android.com

Wouldn't it be better to use multiple APKs for example for API levels <= 8 and >= 9? (Even though it's not recommended by developer.android.com.

    Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    Point screenSize = new Point();

    // calculate the width
    float width;
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
      display.getSize(screenSize);
      width = screenSize.x;
    } else {
      width = display.getWidth();
    }

谢谢!

推荐答案

你要问自己几个问题:

  • 是不是pcated为将要执行此code API的级别去$ P $?
  • 如果是这样,是替代建议或可用?

在你的情况,的getWidth()是pcated赞成使用<德$ P $一个href="http://developer.android.com/reference/android/view/Display.html#getSize%28android.graphics.Point%29"><$c$c>getSize(Point),这需要API级别13,所以之前API级别13,你已经是的getWidth(),这在当时并没有去precated。这些德precated方法保持周围的原因主要是在考虑向后兼容(以及避免破坏它依赖于它的每个人的应用程序)。

In your case, getWidth() is deprecated in favor of using getSize(Point), which requires API level 13. So prior to API level 13, all you have is getWidth(), which at the time was not deprecated. The reason these deprecated methods are kept around is largely with backwards compatibility in mind (along with avoiding breaking everyone's apps which depend on it).

因此​​,要回答你的问题,是的,这是罚款在这种情况下,是的,这是一个很好的使用 @燮pressWarning(下称德precation)

So to answer your questions, yes, this is fine in this case, and yes, it's a good use of @SuppressWarning("deprecation").

这篇关于是否确定在向后兼容code如此多的去precated方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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