七、安卓兼容包

新的安卓应用接口在以前的安卓版本中不起作用,因此引入了安卓兼容性包,允许将新的应用接口移植到旧版本的安卓平台上。本章展示了我们如何使用安卓兼容包。

本章涵盖的主题如下:

  • 我们使用安卓兼容包的原因是什么
  • 如何使用安卓兼容包

什么是安卓兼容包

安卓有一些很棒的新 API,是和 安卓 3.0 及其更高版本一起发布的。然而,许多用户并没有将他们的设备升级到最新的安卓平台。谷歌发布了安卓兼容性包,其中包含对安卓 3.0 及其更高版本发布的一些新应用编程接口的支持。通过这种方式,开发人员可以开发使用新的应用接口并在旧的安卓版本中工作的应用。安卓兼容包中包含的一些类如下:

  • Fragment
  • FragmentManager
  • FragmentTransaction
  • ListFragment
  • DialogFragment
  • LoaderManager
  • Loader
  • AsyncTaskLoader
  • CursorLoader

一些有用的应用编程接口,如动画类、动作栏和片段映射活动,没有包含在安卓兼容包中。

如何使用安卓兼容包

  1. We need to download and install the Android Compatibility Package. In order to download the Android Compatibility Package, click on the Android SDK Manager button in the Eclipse menu as shown in the following screenshot:

    How to use the Android Compatibility Package

    或者,我们可以使用窗口 | 安卓软件开发工具包管理器通过 Eclipse 菜单访问安卓软件开发工具包管理器。打开安卓 SDK 管理器窗口后,勾选安卓支持库选项,如下图截图所示:

    How to use the Android Compatibility Package

  2. Then, click on the Install button and install the package. Now we are ready to develop an Android project that can use the Android Compatibility Package. Firstly, create an Android project in Eclipse. Then, we need to add the support library to our Android project. If it doesn't exist, create a folder named libs under the Android project's root folder as shown in the following screenshot:

    How to use the Android Compatibility Package

  3. Now, find and copy the <your android sdk folder>/extras/android/support/v4/android-support-v4.jar file to the libs folder. The folder structure should be as shown in the following screenshot:

    How to use the Android Compatibility Package

  4. Lastly, if the .jar file is not in the project build path, add the .jar file to the project build path as shown in the following screenshot:

    How to use the Android Compatibility Package

现在您知道如何手动添加支持库了。Eclipse 通过 添加支持库菜单选项使该过程变得容易。使用以下步骤:

  1. 右键单击资源管理器中的项目。
  2. 进入安卓工具 | 添加支持库… 选项。
  3. 按照步骤完成向导。

现在我们可以利用兼容包了。我们将创建一个使用 Fragment类的应用,但是兼容包中存在Fragment类,使用以下步骤显示文本:

  1. 首先,为片段创建布局 XML,并命名 XML 文件fragment.xml :

    ```java <?xml version="1.0" encoding="utf-8"?>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Android Compatibility Package"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    

    ```

  2. 然后,使用以下代码块为活动创建布局:

    ```java

    ```

  3. 现在我们将为fragment.xml布局创建 Fragment类:

    ```java package com.chapter7;

    import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;

    public class Chapter7Fragment extends Fragment {

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment, container, false); return view; } } ```

从前面的代码可以看出,Fragment类来自android.support.v4.app.Fragment包。这意味着我们正在使用安卓兼容包。如果我们不想使用兼容性包,那么我们应该使用来自the android.app.Fragment包的Fragment类。

Activity类对于我们的应用如下:

package com.chapter7;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;

public class Chapter7Activity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        addFragment();
    }

    public void addFragment() {
    FragmentManager fragmentManager = 
 this.getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    Chapter7Fragment fragment = new Chapter7Fragment();
    fragmentTransaction.add(R.id.main_layout,fragment);
    fragmentTransaction.commit();
  }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

从前面的代码块中可以看出,支持库 API 遵循与标准 API 相同的命名。我们只需要使用正确的导入并调用正确的经理。为了使用兼容性包中的类,我们必须将android.support.v4.app添加到我们的导入列表中。

为了得到FragmentManager实例,我们把 称为我们Activity类的getSupportFragmentManager()方法。正如你会注意到的那样,Activity类扩展了 FragmentActivity类。我们需要这样做,因为这是我们使用片段的唯一方法。

AndroidManifest.xml文件应该如下图所示:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.chapter7"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
 android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Chapter7Activity"
            android:label="@string/title_activity_chapter7" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

正如您在这段代码中看到的,最低 SDK 级别被设置为 API 级别 8。我们可以将最低空气污染指数设置为 4 或更高。这样,我们就可以在旧版本的安卓系统中使用新的应用编程接口。

总结

在这一章中,我们学习了什么是安卓兼容包,以及如何使用它。我们还学习了如何在这个库的帮助下,在旧版本的安卓系统中使用新的应用编程接口。

在下一章中,我们将学习使用新的连接应用编程接口——安卓波束和无线直播。