简体   繁体   English

Android:使用目标SDK?

[英]Android: Using Target SDK?

I understand that the minimum SDK defines a minimum required OS level for your application to run and that target SDK tells the device the highest API version you used features from, assuming backwards compatibility has been implemented. 我了解,最低SDK定义了应用程序运行所需的最低操作系统级别,并且假定已实现向后兼容性,目标SDK会告知设备使用的功能的最高API版本。

But that's what I don't understand. 但这是我不明白的。 How do you implement backwards compatibility for entirely new API features? 您如何实现全新API功能的向后兼容性?

For example: 例如:

I've been using the old Notification system during development of my current app, not Notification.Builder. 在开发当前应用程序时,我一直在使用旧的Notification系统,而不是Notification.Builder。 If I choose to use Notification.Builder, will the OS or compiler automatically replace this with compatible code for platforms previous to when this was introduced? 如果我选择使用Notification.Builder,那么操作系统或编译器是否会自动将其替换为兼容的代码,以适应早于引入该平台的平台? Or do I have to check the OS version and write separate code? 还是我必须检查操作系统版本并编写单独的代码?

You have to account for the new methods yourself, for example by using, 您必须自己考虑新方法,例如通过使用,

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    // Use methods introduced in API 11. (i.e. Notification.Builder)
} else {
    // Use old methods to ensure backwards compatibility.
}

A nice place to put these checks is in a utility class (ie CompatUtils.java ). 进行这些检查的一个好地方是在实用工具类中(即CompatUtils.java )。 So for example, you might create a static method that takes a Context argument and returns a new Notification : 因此,例如,您可以创建一个带有Context参数并返回新Notification的静态方法:

public static buildNotification(Context ctx) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // Create and return the Notification using Notification.Builder
    } else {
        // Create and return the Notification using pre-HoneyComb methods
    }
}

Then simply call CompatUtils.buildNotification(this) within your Activity to create the new Notification . 然后只需在Activity调用CompatUtils.buildNotification(this)即可创建新的Notification Abstracting out these sorts of details keeps your Activity concise, and is easily changeable if you ever need to later on. 提取出这些细节可以使您的Activity保持简洁,并且在以后需要时可以轻松更改。

Of course, it might be a better idea to just use the old methods to create your Notification , since you need to implement them anyway and the Notification.Builder is mostly just for convenience. 当然,最好只使用旧方法来创建Notification ,因为您还是需要实现它们,而Notification.Builder只是为了方便起见。 Nonetheless, this is definitely something that you should keep in mind in case you run into similar issues later on! 但是,绝对要牢记这一点,以防以后遇到类似问题!


Edit: 编辑:

Notification.Builder is also provided in the Android Support Package, so you should just use that in this case... however, for methods that are not provided in the compatibility package, you should use the design pattern described above. Android支持包中还提供了Notification.Builder ,因此在这种情况下,您只应使用它...但是,对于兼容性包中提供的方法,应使用上述设计模式。

google has its own compatability packages (in the support library) and there are also other third party solutions (like actionBarSherlock) , and the Lint tool can tell you when you're trying to use a too-new feature that won't work on the API range you've set. google有自己的兼容性软件包(在支持库中),还有其他第三方解决方案(例如actionBarSherlock),当您尝试使用无法使用的新功能时,Lint工具可以告诉您您设置的API范围。

one way to check the API is using the android.os.Build.VERSION.SDK_INT . 检查API的一种方法是使用android.os.Build.VERSION.SDK_INT。 another way is by using reflection , which sometimes google uses (yet i'm not sure why) . 另一种方法是使用反射,谷歌有时会使用反射(但我不确定为什么)。

in any case , google suggests in its videos to always set the targetSdk to the highest one that there is, in order to improve the optimization of the output APK. 无论如何,谷歌建议在其视频中始终将targetSdk设置为存在的最高视频,以改善输出APK的优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM