In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, Xiaobian will bring you about how to call the high version API in the low version SDK. The article is rich in content and analyzes and narrates from a professional perspective. After reading this article, I hope you can gain something.
As Android SDK versions are iteratively updated, there are always eye-catching, easy-to-use APIs on new SDKs. However, these new APIs are not available in earlier versions of the SDK, so when our minSdkVersion is lower than the version of the new API, we will directly use the new API and an error will occur during compilation. If you just want to solve the compilation error problem, Android provides us with two ways to avoid compilation errors:
@SuppressLint
@TargetApi
What is the difference between these two methods?
. SupressLint and TargetApi
SuppressLint obviously means ignoring Lint checking, for us to use a higher version API, we can use @SuppressLint("NewApi") to make Lint ignore the version requirements of the API called at compile time. @TargetApi is an error that ignores a particular version of the API call.
For example, when your project minSdkVersion=9, you want to use API 11's new method. In this case, using @TargetApi(11) and @SuppressLint("NewApi") has the same effect, and the code does not report errors. But if you use a new method that only appeared in Api 19, it will still give you an error in the @TargetApi method, but it will still not give you an error in the other method.
How about @SuppressLint("NewApi")?
. Correct posture
Of course not! We should be clear that using the above two comments only makes lint no longer report errors at compile time. In a lower version of the mobile phone system, if you directly use a higher version of the API, it will definitely report: Crash of "NoSuchMethod".
Therefore, the correct approach should be to do version judgment in the annotated method, and still use the old way to deal with it in the lower version. When judging the version, we need to judge the specific version number, such as
1@TargetApi(9)
2public void doSomeThing() {
3 if(Build.VERSION.SDK_INT >= 9) {
4 //At this point we normally use API 9 methods, if you use the API 11 method incorrectly, you will report an error when compiling.
5 //remind us that we are only introducing methods from API 9
6 } else {
7 // TODO uses the old way
8 }
9}
10
11@SuppressLint("NewApi")
12public void doOthers() {
13 if(Build.VERSION.SDK_INT >= 9) {
14 //At this point we normally use API 9 methods, if the method in Api 11 is used incorrectly here, the compilation will not report an error.
15 //then running in a lower version will cause a Crash risk
16 } else {
17 // TODO uses the old way
18 }
19}The above is how to call the high version API in the low version SDK shared by Xiaobian. If you happen to have similar doubts, you may wish to refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.