ZetCode

第一个 Android 应用程序

最后修改于 2012 年 12 月 5 日

在本章的 Android 开发教程中,我们将创建我们的第一个 Android 应用程序。

该应用程序将在屏幕上显示一条消息。

$ mkdir First
$ cd First/

我们创建一个 `First` 目录并将其设为当前工作目录。

$ android create project --target android-17 --name First \
> --path . --activity MainActivity --package com.zetcode.first
Created directory /home/janbodnar/programming/android/First/src/com/zetcode/first
Added file ./src/com/zetcode/first/MainActivity.java
Created directory /home/janbodnar/programming/android/First/res
Created directory /home/janbodnar/programming/android/First/bin
...

我们使用 `android create project` 命令创建一个新的 Android 项目。target 选项指定了 Android 应用程序框架版本。name 选项确定了项目的名称。path 是我们项目目录的位置。activity 是我们默认 activity 的名称。最后,package 是我们项目包命名空间的名称。该命令会为主 activity、几个目录和 XML 文件创建一个 Java 文件。

$ ls 
AndroidManifest.xml  bin        libs              proguard-project.txt  res
ant.properties       build.xml  local.properties  project.properties    src

这些是 `android create project` 命令创建的文件和目录。`AndroidManifest.xml` 文件描述了应用程序的基本特性。应用程序的源代码位于 `src` 目录中。在 `res` 目录中,我们有应用程序的资源文件。Android 应用程序归档文件将在 `bin` 目录中创建。`libs` 目录用于存储附加库。`ant.properties` 和 `build.xml` 是用于构建项目的 Ant 文件。最后,`local.properties` 和 `project.properties` 是 Android 项目的属性文件。

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.zetcode.first"
      android:versionCode="1"
      android:versionName="1.0">
   <application android:label="@string/app_name"
                   android:icon="@drawable/ic_launcher">
       <activity android:name="MainActivity"
                 android:label="@string/app_name">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>
   </application>
</manifest>

这是项目清单文件。它描述了 Android 项目的一些基本特性。该文件提供了包名,在本例中是 com.zetcode.com。它包含了默认 activity 的名称。`@string/app_name` 和 `@drawable/ic_launcher` 是资源值。字符串资源值从位于 `res/values` 子目录的 `strings.xml` 文件中设置。图像资源位于 res 目录的 `drawable` 子目录中。主 activity 的 `` 元素声明了它的能力。它指定了 activity 可以做什么。两个 intent 指定了该 activity 是应用程序的主入口点,它可以是任务的初始 activity,并且列在顶层应用程序启动器中。

strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">First program</string>
    <string name="messsage">First Android program</string>
</resources>

在 `strings.xml` 文件中,我们有一个元素定义了从 `AndroidManifest.xml` 文件引用的资源值。该文件位于 `res/values` 子目录中。我们更改第一个元素的值(从“MainActivity”到“First program”)。应用程序的名称显示在模拟器中的应用程序列表中。我们添加第二个元素。它从 `main.xml` 文件中引用。

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/message"
    />
</LinearLayout>

这是位于 `res/layout` 子目录的 `main.xml` 文件。它定义了 `Activity` 的布局。应用程序在 `onCreate()` 方法中加载 `Activity` 的布局。在本例中,我们有一个带有 `TextView` 控件的垂直线性布局。

MainActivity.java
package com.zetcode.first;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

这是 `MainActivity.java` 源文件。当 activity 首次创建时,会调用 `onCreate()` 方法。该方法加载在 `main.xml` 文件中定义的 activity 布局。

构建应用程序

我们使用 `ant` 工具来构建 Android 应用程序。

$ ant debug

我们在项目根目录中执行 `ant debug` 命令。有两种构建目标。Debug 和 Release。我们将使用 debug 构建目标。Release 构建需要一些额外的签名工作。

$ ant debug install

可以使用 `ant debug install` 命令一步构建和安装应用程序。

$ ls -1 bin
AndroidManifest.xml
AndroidManifest.xml.d
build.prop
classes
classes.dex
classes.dex.d
First.ap_
First.ap_.d
First-debug.apk
First-debug-unaligned.apk
First-debug-unaligned.apk.d
jarlist.cache
proguard.txt
res

最终的 Android 包创建在 bin 目录中。我们的归档文件名是 `First-debug.apk`。

运行应用程序

我们将应用程序安装到模拟器上,并从模拟器启动它。

$ emulator -avd AVD2 &

模拟器以特定的 Android 虚拟设备启动。

$ adb install bin/First-debug.apk 

`adb install` 命令将应用程序安装在模拟器上。

First program
图:第一个程序

我们没有使用自定义图标,所以使用了内置图标。从应用程序列表中,我们选择“First program”应用程序。该应用程序启动。

First program
图:First program 屏幕

我们可以看到我们在 `strings.xml` 文件中指定的这条消息。

本章是 Android 应用程序开发入门。