如何code向后兼容的新功能,在Android SDK中? [英] How to code backward compatible new feature in Android SDK?

查看:162
本文介绍了如何code向后兼容的新功能,在Android SDK中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用包含在SDK 11的动作条功能,但是我也希望应用程序来从SDK 10(2.3.3)更早的设备上运行。我愿意放弃动作条功能的早期设备,因为它不是一个重要特征。我已经做了所有关于反省,包装类和其他一些技术的阅读。我现在难倒究竟如何使这项工作。我使用的Eclipse。

I want to use the actionbar feature included in SDK 11. However I also want the app to run on earlier devices from SDK 10 (2.3.3). I am willing to give up the actionbar feature for the earlier devices as it is not an important feature. I have done all the reading about reflection, wrapper class and some other techniques. I am now stumped on exactly how to make this work. I am using Eclipse.

如果我没有设置在Eclipse中目标SDK 11或更高,那么我有一个参考,以动作条的任何地方给人编译错误。如果我把到SDK 11或更高它编译目标,但不会表明它可以在更早的设备上运行。我有机器人:的minSdkVersion = 10 将所有的时间

If I don't set the target in Eclipse to sdk 11 or greater, then any place I have a reference to actionBar gives a compile error. If I put the target to sdk 11 or greater it compiles but won't show that it can run on earlier devices. I have android:minSdkVersion=10 set all the time.

有人可以给我如何使引用动作条,但得到它的一个目标previous SDK级别一些见解?先谢谢了。

Can someone give me some insight on how to make the references to actionBar and yet get it to target a previous sdk level? Thanks in advance.

推荐答案

是的!你绝对可以做到这一点。尝试下面列出的模式。

Yes! You can definitely do this. Try following the pattern outlined below.

在你的的Andr​​oidManifest.xml 文件中声明如下(更换平台的版本,无论你的应用程序需要):

In your AndroidManifest.xml file declare the following (replacing the platform versions with whatever your app requires):

<!-- Build Target -->
<uses-sdk android:targetSdkVersion="14" android:minSdkVersion="7" />

通过针对API 11或更高的平台版本,您允许Eclipse的链接(编译)对本机动作条类。提供了早期最低的平台版本,使您的应用程序将在旧版本的Andr​​oid安装(运行)。

By targeting a platform version of API 11 or higher, you are allowing Eclipse to link (compile) against the native ActionBar classes. Providing an earlier minimum platform version allows your app to be installed (run) on older versions of Android.

您活动code然后应该是这个样子:

Your Activity code should then look something like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (CompatibilityManager.isHoneycomb()) {
        final ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowHomeEnabled(true);
        // ...
    } else {
        // The ActionBar is unavailable!
        // ...
    }
}

CompatibilityManager.java 类只提供静态辅助方法来确定SDK的当前版本:

Where the CompatibilityManager.java class simply provides static helper methods for determining the current version of the SDK:

public class CompatibilityManager {
    public static final String KINDLE_FIRE_MODEL = "Kindle Fire";

    /**
     * Get the current Android API level.
     */
    public static int getSdkVersion() {
        return android.os.Build.VERSION.SDK_INT;
    }

    /**
     * Determine if the device is running API level 11 or higher.
     */
    public static boolean isHoneycomb() {
        return getSdkVersion() >= Build.VERSION_CODES.HONEYCOMB;
    }

    /**
     * Determine if the device is running API level 14 or higher.
     */
    public static boolean isIceCreamSandwich() {
        return getSdkVersion() >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
    }

    /**
     * Determine if the current device is a first generation Kindle Fire.
     * @return true if the device model is equal to "Kindle Fire", false if otherwise.
     */
    public static boolean isKindleFire() {
        return Build.MODEL.equals(KINDLE_FIRE_MODEL);
    }
}

您也可以考虑充分利用 ActionBarSherlock 库,它提供了一个兼容的ActionBar API的所有返回的途中到Android 2.X:

You might also consider leveraging the ActionBarSherlock library, which provides a compatible ActionBar API all the way back to Android 2.x:

该库将自动使用本机的操作栏时,   可还是会自动折回自定义实现   您的布局。这使您可以方便地开发与应用   操作栏中为每个版本的Andr​​oid背透2.x的。

The library will automatically use the native action bar when available or will automatically wrap a custom implementation around your layouts. This allows you to easily develop an application with an action bar for every version of Android back through 2.x.

玩得开心!

这篇关于如何code向后兼容的新功能,在Android SDK中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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