470 likes | 620 Views
Ch2 初探 Android 程式開發. 一、 建立一個簡單的應用程式專案 (2-1). 啟動 Eclipse 。 執行「 File>New>Android Application Project 」選項 。 設定專案相關資訊 。 設定專案架構 。 設定專案圖示 。 選擇要建立的活動 (Activity) 類型 。 設定活動內容. 啟動 Eclipse. 啟動 Eclipse. 執行「 File>New>Android Application Project 」選項. 設定專案相關資訊 (1). 設定專案相關資訊 (2). 設定專案架構. 設定專案圖示.
E N D
一、建立一個簡單的應用程式專案(2-1) • 啟動Eclipse 。 • 執行「File>New>Android Application Project」選項。 • 設定專案相關資訊。 • 設定專案架構。 • 設定專案圖示。 • 選擇要建立的活動(Activity)類型。 • 設定活動內容
修改Android Target 版本 • 右擊專案檔名 • 選Properities/Android • 選擇適當版本
二、模擬器設定 • 從Eclipse執行「Window>Android Virtual Device Manager」。 • 按右上角的「New」鈕,以建立一個新的AVD。 • 設定AVD參數。 • 將新建的AVD增至對話框的AVD清單中。
三、以AVD測試應用程式專案(2-1) • 開啟「Android Virtual Device Manager」對話框。 • 選要使用的AVD,按「Start」鈕啟動AVD 。 • 按「Lunch」鈕關閉對話框並載入此AVD 。 • Eclipse IDE開始模擬此AVD 。 • 模擬裝置完成載入,以滑鼠敲「OK」鈕啟動此模擬裝置。 • 關閉「Android Virtual Device Manager」對話框。
建立AVD且彈出視窗後,須將後端之屬性是窗關閉,方能使用Eclipse之Run/Run as configuration
三、以AVD測試應用程式專案(2-2) • 從Eclipse主畫面左面板選應用程式專案,再按工具列上的「Run Activity_main.xml」工具鈕。 • 從開啟的「Run AS」對話框中選「Android Application」,然後按「OK」鈕。 • 以滑鼠按模擬裝置的電源鈕,模擬將裝置關電。
四、以實體裝置測試應用程式專案 • 啟動實體裝置的「USB除錯」功能。 • 透過USB將實體裝置連至執行Eclipse的電腦。 • 從Eclipse主畫面左面板選應用程式專案,再執行「Run>Run As>1.Android Application」選項。 • 從開啟的「Android Device Chooser」對話框,先選「Choose a running Android device」(連至電腦執行中的實體裝置),然後從顯示的清單中點選你的實體裝置。
安裝USB驅動程式說明 • http://www.sogi.com.tw/mobile/articles/6090212-%E4%B8%8B%E8%BC%89%E4%B8%A6%E5%AE%89%E8%A3%9D+Android+USB+%E9%A9%85%E5%8B%95%E7%A8%8B%E5%BC%8F
在實體裝置上測試 • 於手機上點擊 [設定] • 點選[開發人員選項] • 點選[USB偵錯] • ?允許USB偵錯嗎?[確定]
五、Android專案架構剖析(2-1) • src:此資料夾用以存放原始程式檔。 • gen:存放ADT自動產生的java程式檔的資料夾,其中最重要的就是R.java,R.java內建立一個R類別,此類別為res資料夾中的每一個資源指定了唯一的ID,程式透過這ID就可引用它對應的資源。 • assets:此資料夾預設是空的,你可以在這裡放置一些原始檔案。
五、Android專案架構剖析(2-2) • bin:專案建置後產生的輸出資料夾,包括了應用程式建置後的安裝封包.apk檔,及其他編譯過(compiled)的資源。 • libs:用以存放私有的函式庫檔案的資料夾。 • res:用以存放應用程式專案所需要資源的資料夾。 • AndroidManifest.xml:專案的組態檔。 • Project.properties:此檔案包含了專案的設定。
Android Manifest.xml檔案 • <?xml version="1.0" encoding="utf-8"?> • <manifest xmlns:android="http://schemas.android.com/apk/res/android" • package="com.example.newtest" • android:versionCode="1" • android:versionName="1.0" > • <uses-sdk • android:minSdkVersion="8" • android:targetSdkVersion="18" /> • <application • android:allowBackup="true" • android:icon="@drawable/ic_launcher" • android:label="@string/app_name" • android:theme="@style/AppTheme" > • <activity • android:name="com.example.newtest.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>
MainActivity.java • package com.example.newtest; • import android.os.Bundle; • import android.app.Activity; • import android.view.Menu; • public class MainActivity extends Activity { • @Override • protected void onCreate(Bundle savedInstanceState) { • super.onCreate(savedInstanceState); • setContentView(R.layout.activity_main); • } • @Override • public boolean onCreateOptionsMenu(Menu menu) { • // Inflate the menu; this adds items to the action bar if it is present. • getMenuInflater().inflate(R.menu.main, menu); • return true; • } • }
Activity_main.xml檔 • <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" • xmlns:tools="http://schemas.android.com/tools" • android:layout_width="match_parent" • android:layout_height="match_parent" • android:paddingBottom="@dimen/activity_vertical_margin" • android:paddingLeft="@dimen/activity_horizontal_margin" • android:paddingRight="@dimen/activity_horizontal_margin" • android:paddingTop="@dimen/activity_vertical_margin" • tools:context=".MainActivity" > • <TextView • android:layout_width="wrap_content" • android:layout_height="wrap_content" • android:text="@string/hello_world" /> • </RelativeLayout>
Strings.xml檔案 • <?xml version="1.0" encoding="utf-8"?> • <resources> • <string name="app_name">newTest</string> • <string name="action_settings">Settings</string> • <string name="hello_world">Hello world!</string> • </resources>
Sample Code • Import the sample project: • Select File > New > Other > Android Sample Project and click Next. • Select the latest version of the Android SDK and click Next. • Scroll down the list of samples and select Maps [Google Play Services]. => Example • Click Finish. • https://developers.google.com/maps/documentation/android/intro?hl=zh-TW