Android的向后兼容性,但还是利用最新的API功能 [英] Android backward compatibility but still utilise latest API features

查看:223
本文介绍了Android的向后兼容性,但还是利用最新的API功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android Market中指出,许多流行的应用程序具有向后兼容更早版本的Andr​​oid。 例如,

I have noted in the Android Market that many popular applications have backward compatibility to much earlier versions of Android. E.g.

Evernote - 1.6
Faceobook Messenger - 2.2

这些应用程序的外观和工作的伟大,但他们怎么能做到这一点,并支持更古老的API级别?是它们仅在使用中存在的最低支持的操作系统版本API功能,几乎不?我假设他们必须使用后的API级别的某些功能提供了极大的UI和featurelist。

These applications look and work great but how can they do this and support much older API levels? Are they only using API features that exist in the lowest supported OS version, hardly? I'm assuming they must be using some features of the later API levels to provide a great UI and featurelist.

我可以看到两个可能的解决方案:

I can see two possible solutions:

使用最小/目标API级别的构建。然后通过code您检查操作系统版本,并实现使用支持方法的特点和适度地降低。这似乎是一个大量的工作。

有针对不同操作系统的多个版本的应用程序版本。例如。一个版本2.2,另一个为4.0。这可能吗?

之所以问的是我策划一个新的应用程序,应该支持2.2,但我担心我可能需要的API功能,这些功能仅在以后的版本可用?如果我只是针对2.2?

The reason for asking is I am planning a new app that should support 2.2 but I fear I may require API features that are only available in later releases? Should I just target 2.2?

编辑:另外,也兼容库扮演什么样的角色?这是关键?

Also, what role do Compatibility Libraries play? Is this the key?

感谢。

推荐答案

我们(Evernote的)做额外的工作来支持1.6,并使用许多新的API,我们可以。与之配套1.6主要问题是Dalvik的做你的班贪婪搜索。这使得它无法使用code像

We (Evernote) do extra work to support 1.6 and use as many new API's as we can. The main problem with supporting 1.6 is that Dalvik does a greedy search on your classes. This makes it impossible to use code like

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
    prefEditor.apply();
} else {
    prefEditor.commit();
}

由于它会抛出一个类验证错误。这时候的Dalvik看到你的方法,并尝试在运行时访问它引起的。

Because it would throw a class verification error. This is caused when dalvik sees your method and tries to access it at runtime.

相反,你需要使用一个辅助类实例化适当的类的SDK。是的,这是更多的工作。

Instead you need to use a helper class to instantiate the appropriate class for the SDK. Yes this is much more work

public abstract class SharedPreferenceEditor {

  private static SharedPreferenceEditor sInstance;

  public static SharedPreferenceEditor getInstance() {
    if (sInstance == null) {

      /*
      * Check the version of the SDK we are running on. Choose an
      * implementation class designed for that version of the SDK.
      */
      @SuppressWarnings("deprecation")
      int sdkVersion = Build.VERSION.SDK_INT;
      if(Evernote.DEBUG)Log.d("SharedPreferenceEditor", "sdkVersion=" + sdkVersion);
      if (sdkVersion < Build.VERSION_CODES.GINGERBREAD) {
        sInstance = new CommitSharedPreferenceEditor();
      } else  {
        sInstance = new ApplySharedPreferenceEditor();
      }
    }
    return sInstance;
  }

  public abstract void save(SharedPreferences.Editor editor);
}

然后你有一个为姜饼+ API级别

Then you have one for the gingerbread + api level

public class ApplySharedPreferenceEditor extends SharedPreferenceEditor {
  public void save(SharedPreferences.Editor editor) {
    editor.apply();
  }
}

和一个用于在&lt;姜饼水平

and one for the < gingerbread level

public class CommitSharedPreferenceEditor extends SharedPreferenceEditor{
  public void save(SharedPreferences.Editor editor) {
    editor.commit();
  }
}

我会建议支持2.1及更高版本,以便比你可以采取的改善Dalvik的优势,并利用我所列出的第一个例子。

I'd recommend supporting 2.1 and up so than you can take advantage of the improvements to Dalvik and use the first example I listed.

这篇关于Android的向后兼容性,但还是利用最新的API功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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