Android learning route (19th) Supports different devices-Supports different (Android) platform versions and android routes

Source: Internet
Author: User
Tags xml attribute

Android learning route (19th) Supports different devices-Supports different (Android) platform versions and android routes

When the latest Android version provides great APIs for your application, you need to continue to support the old system version before more devices are updated. This course describes how to use the new advanced API when you continue to support earlier versions of the system.

The Platform Versions dashboard displays the distribution of Android system Versions running on the latest active devices, based on the number of times devices access the Google Play Store. Generally, it is a good habit to support 90% active devices and use the latest version as the target.

TIPS:To provide the best features and functions in different Android versions, you should use the Android Support Library in your application, it allows you to use some recent versions of APIs on earlier versions.

Specify the minimum and target API levels

The AndroidManifest. xml file describes the details of your application and the supported system versions. In particular, the <use-sdk ElementminSdkVersionAndtargetSdkVersionThe attribute specifies the minimum API level for the application and the highest API level you designed and tested for the application.

For example:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" ... >    <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="15" />    ...</manifest>

Due to the release of the new Android system, some styles and behaviors may change. To allow your application to take advantage of these changes and ensure that your application uses the device of each user, you needtargetSdkVersionThe value is set to the lowest available Android version.

Check the system version at runtime

AndroidBuildThe constant class provides a unique identifier. Use this ID code in your application to create conditions so that the code at the High API level can be executed only when supported by the system version.

private void setUpActionBar() {    // Make sure we're running on Honeycomb or higher to use ActionBar APIs    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {        ActionBar actionBar = getActionBar();        actionBar.setDisplayHomeAsUpEnabled(true);    }}

Tip:When parsing XML resources, the Android system ignores XML attributes that are not supported by the current device. Therefore, you can use the XML Attribute of the latest version safely without worrying about the crash of the old version. For example, if you settargetSdkVersion="11"Your app is included in Android 3.0 or later by default.ActionBar. Then add a menu item for the action bar. You need to set it in the resource file under your menu.android:showAsAction="ifRoom". It's okay to do this in a cross-version XML file, because Android of the old version directly ignores this attribute (in this case, you do not need to create a separateres/menu-v11/Directory ).

Use platform styles and themes

Android provides users with a user experience topic, which gives users the interface and feeling of the underlying operating system of the application. These topics can be applied in your application list file. By using these built-in styles and themes, your application will have the UI and feeling of each release version.

Make your activity look like a dialog box:

<activity android:theme="@android:style/Theme.Dialog">

Make your activity background transparent:

<activity android:theme="@android:style/Theme.Translucent">

Apply your/res/values/styles.xmlCustom topic defined in:

<activity android:theme="@style/CustomTheme">

To apply a topic to the entire application (all activities ),<application>Add the android: theme attribute to the element:

<application android:theme="@style/CustomTheme">

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.