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

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