Android學習筆記(四) Hello World

來源:互聯網
上載者:User

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的介面;

Ø 配置該介面,例如下例:

Ø 點擊“Create AVD”,完成!

1.2.3 建立一個Android工程

建立一個AVD後,我們得建立一個Hello World工程:

Ø Eclipse中選擇功能表項目File > New > Project;

Ø 選擇如下的“Android Project”,按 Next。若未出現“Android Project”,則說明ADT安裝失敗;

Ø 在“New Android Project”面板中填入相應資訊,如下,後點擊 Finish 按鍵完成建立工作:

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可能要比較久的時間,請耐心等待。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.