Compatibility processing between different versions of Android apps

Source: Internet
Author: User

Backwards compatibility is poor in Android, but an app app can be processed or run between versions. Backward compatibility is not good, different versions of the system its API version is different, naturally some interfaces are different, the new platform can not use the old API, the old platform can not use the new API.

For the app to be more compatible, we can use the high version of the SDK to develop the application, and in the program runtime (runtime) on the platform to run the application judgment, the old platform using the old API, and the new platform can use the new API, which can improve the software compatibility.

So how do you make such judgments when the software is running? Bottom of the answer:

There are a few words in the Android SDK Development documentation:

Check system version at runtime (checks to determine the version of the software at run time)

Android provides a unique code for each platform version in the Build constants class. Use these codes within your apps to build conditions that ensure the code thatdepends on higher API levels are executed only When those APIs is available on the system.

Private voidSetupactionbar() { Make sure we ' re running on honeycomb or higher to use ActionBar APIs If  (build. Version. Sdk_int >= build.. Honeycomb  {  Actionbar ActionBar = Getactionbar (); Span class= "PLN" > Actionbar.true }}          

Note: When parsing XML resources, Android ignores XML attributes that aren ' t supported by the current device. So-can safely use XML attributes Thatare only supported by newer versions without worrying about older versions Breaki ng when theyencounter to that code. For example, if you set targetSdkVersion="11" the, your apps includes the by ActionBar Defaulton Android 3.0 and higher. To then add menu items to the action bar, you need to set in android:showAsAction="ifRoom" your menu resource XML. It's safe to do this in a cross-version XML file, because the older versions of Android simply ignore the showAsAction attribute Need a separate version in res/menu-v11/ ).

As you can see from above, Android provides us with a constant class build, most notably the two internal class version and Version_codes in Build,

Version represents the current system version information, which includes the SDK version information for member Sdk_int representation;

For Version_codes described in the SDK development documentation, enumeration of the currently known SDK version codes. These is the values that can is found in SDK . Version numbers increment monotonically with each official platform release.

Its members are some version number constants from the earliest version to the currently running system.

In our own development of the application process, we often use the following code form to determine whether to run the new API or the old API:

    if (Build.VERSION.SDK_INT >= build.version_codes. Honeycomb)     {            //  code block containing the new API     }    else    {             //  code block containing the old API    }

OK, everybody knows the principle! Baidu needs to be a lot of examples, here is not provided.

Original: http://blog.csdn.net/leichelle/article/details/7988561

Compatibility processing between different versions of Android apps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.