Android学习笔记(四) Hello World

1.1 SDK概览

在我们通过“Android SDK and AVD Manager”工具增加必要的组件后,我们可以进入SDK根目录看看其概况,如下:

Name

Description

add-ons/

Contains add-ons to the Android SDK development environment, which let you develop against external libraries that are available on some devices. 主要是一些Google API

docs/

A full set of documentation in HTML format, including the Developer's Guide, API Reference, and other information. To read the documentation, load the file offline.html in a web browser.

platforms/

Contains a set of Android platform versions that you can develop applications against, each in a separate directory.

 

<platform>/

Platform version directory, for example "android-1.6". All platform version directories contain a similar set of files and subdirectory structure.

   

data/

Storage area for default fonts and resource definitions.

   

images/

Storage area for default disk images, including the Android system image, the default userdata image, the default ramdisk image, and more. The images are used in emulator sessions.

   

skins/

A set of emulator skins available for the platform version. Each skin is designed for a specific screen resolution.

   

templates/

Storage area for file templates used by the SDK development tools.

   

tools/

Any development tools that are specific to the platform version.

   

android.jar

Java 归档文件,其中包含构建应用程序所需的所有的 Android SDK 类。

samples/

Sample code and apps that are specific to platform version.

包含各种应用程序的源代码,包括 ApiDemo,该应用程序演示了很多 API。这个示例应用程序可以作为 Android 应用程序开发的良好起点。

tools/

包含所有用于构建 Android 应用程序的命令行工具。最常用、最有用的工具是 adb 实用程序(Android Debug Bridge)。

usb_driver

该目录包含将开发环境连接到支持 Android 的设备(例如 G1 或 Android Dev 1 解锁开发手机)所需的驱动程序。只有 Windows 平台的开发人员才需要这些文件。

SDK Readme.txt

A file that explains how to perform the initial setup of your SDK, including how to launch the Android SDK and AVD Manager tool on all platforms

SDK Setup.exe

Windows SDK only. A shortcut that launches the Android SDK and AVD Manager tool, which you use to add components to your SDK.

1.2 Hello World

参考文章:Hello World tutorial

1.2.1 先决条件

Ø 已安装SDK,并且至少安装了一个SDK Platform;

Ø 安装了Eclipse以及ADT,若是其他IDE的话,请先参考:Developing in Other IDEs

1.2.2 新建一个AVD

AVD是Android Virtual Devices(Android虚拟设备)的简称。更详细信息请参考Android Virtual Devices

Android自带了一个模拟器,我们所有的应用程序均可在上面运行。当然,若是从事Linux内核移植和驱动开发的话,模拟器可能用处不大。

在启动模拟器前,必须新建一个AVD。AVD定义了系统镜像以及设备的设置。新建AVD步骤如下:

Ø 在Eclipse,选择菜单项Window > Android SDK and AVD Manager;

Ø 选择左边面板中的“Virtual Devices”,并点击New,则出现 新建AVD的界面;

Ø 配置该界面,例如下例:

clip_image002

Ø 点击“Create AVD”,完成!

1.2.3 新建一个Android工程

新建一个AVD后,我们得新建一个Hello World工程:

Ø Eclipse中选择菜单项File > New > Project;

Ø 选择如下的“Android Project”,按 Next。若未出现“Android Project”,则说明ADT安装失败;

clip_image004

Ø 在“New Android Project”面板中填入相应信息,如下,后点击 Finish 按键完成新建工作:

clip_image006

1.2.4 新建工程中各个field的说明(可略)

Ø Project Name

This is the Eclipse Project name — the name of the directory that will contain the project files.

Ø Application Name

This is the human-readable title for your application — the name that will appear on the Android device.

Ø Package Name

This is the package namespace (following the same rules as for packages in the Java programming language) that you want all your source code to reside under. This also sets the package name under which the stub Activity will be generated.

Your package name must be unique across all packages installed on the Android system; for this reason, it's important to use a standard domain-style package for your applications. The example above uses the "com.example" namespace, which is a namespace reserved for example documentation — when you develop your own applications, you should use a namespace that's appropriate to your organization or entity.

Ø Create Activity

This is the name for the class stub that will be generated by the plugin. This will be a subclass of Android's Activity class. An Activity is simply a class that can run and do work. It can create a UI if it chooses, but it doesn't need to. As the checkbox suggests, this is optional, but an Activity is almost always used as the basis for an application.

Ø Min SDK Version

This value specifies the minimum API Level required by your application. For more information, see Android API Levels .

Ø Other fields

The checkbox for "Use default location" allows you to change the location on disk where the project's files will be generated and stored. "Build Target" is the platform target that your application will be compiled against (this should be selected automatically, based on your Min SDK Version).

1.2.5 构建UI界面

参考以下修改后的源码,并“依葫芦画瓢”来修改自动生成的HelloAndroid 类。

package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloAndroid extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       TextView tv = new TextView(this);
       tv.setText("Hello, Android");
       setContentView(tv);
   }
}

Ø 一个小技巧:

增加import packages 最简单的方法是按下Ctrl-Shift-O (Cmd-Shift-O, on Mac)。它会根据代码的需要自动增加缺失的package;

1.2.6 运行应用程序

Eclipse的插件使得运行我们的应用相当容易。

选择菜单项 Run > Run,接着选择 "Android Application"。

根据PC配置的不同,模拟器中启动Hello World可能要比较久的时间,请耐心等待。

clip_image008

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章