1
Android beginning
NCCU SSRCWilliam Hong 洪維林2009.12
2
Agenda Basic concept
History of Android Android Architecture Application fundamentals Application Components
Setup Environment Preparing stuff( 需要的安裝檔案 & 簡要說明安裝程序 ) PDA Driver 安裝 Android SDK Directory Google Market( 自己開發的程式 , 加入 Market)
Let’s start coding Android 專案 : HelloCS AndroidManifest.xml UI Layout xml U2ex architecture Reference
3
Basic ConceptHistory of AndroidAndroid ArchitectureApplication fundamentalsApplication Components
4
History of Android ‘05/8 :
Google 收購 Android 公司 , Google 邁入 mobile 的世界Android 公司 , 原本即開發手機 OS
‘07 :開放手機聯盟成立 & 發布第一版 Android SDK
‘08 :舉辦 2008 開發競賽 & Market 上線 ’08/9/23 : HTC G1 上市 & Android 1.0 SDK
release ’09/10 : Android 2.0 SDK release
5
Android Architecture藍色 : Java
6
Applications
包含內建的應用程式 - 聯絡人、 eMail 、 Map 、 Calendar 、簡訊…等等
從 Market 下載的程式 自行開發的程式 以 Java Programming Language 開發
7
Application Framework
Google 提供 API, 此 framework 內已經具備多種不同的基礎軟體元件,在開發 ap 時,可直接使用
內容可參考網頁
8
Libraries
Library 以 c/c++ 開發 屬系統元件 , 開發者可以透過 Application
Framework 來使用這些功能
9
Android Runtime
Core Libraries 對應於 Java Programming Language
Dalvik Virtual Machine ( 簡稱 DVM) 開發者 : Dan Bornstein, 以某本小說中談論的冰島 Dalvik 命名 針對手機開發 Run( 綁 ) on Linux Virtual Memory, multiple processes 執行指令精簡 ( 節省記憶體 )&CPU 效率 (not JIT) 詳細說明 , 請看連連
10
Linux Kernel
Base on Linux 2.6 已修改 kernel, 以避免 GPL 授權問題 ( 連結)
Linux 版權 GNU General Public License version 2 (GPLv2)
Driver 移動至 linux userspace, 硬體廠商不需要公開driver source
Power Management 修改 針對手機環境 沒有使用 , 就關掉
11
Application fundamentals 使用 Java language 開發 編譯完成 , 以 tool/aapt.exe 包裹為 .apk Apk application 三種基本特性
每個 application 在自己的 Linux process 中執行 每個 process 有自己的 Java VM, isolate from other
applications 每個 application 權限獨立分開 , 不能讀取其他
application 的檔案 若兩個 application 具備相同的 linux id, 即可以
share VM & files ( 少用 )
12
Application Components 在 framework 之上進行開發 , 所以沒有
main(), 且由 framework 主控全局 溝通橋樑: Intent 四大元件: Android 程式四大塊
ActivityServiceBroadcast receiverContent provider
要明確定義於: AndroidManifest.xml
13
Intent 用來描述一個程式想要作些什麼事情 每個 Intent 都帶有一個動作 (action) ,並根據不
同的動作去行動 例
public void onClick(){ Uri uri = Uri.parse("http://www.nccu.com.tw/"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent);}
要開啟一個網頁 , 由 Android 去決定誰要開網頁
14
Activity 包括 UI, 以及與 user 互動 上面可以放
button 、 list 、 picture 、 text… UI 可動態調整 ( 增加、減少、換位置 ) 透過 intent 跳轉至其他 activity 費時的程式要放到 Service, handler…, 超
過 5 秒 , 會出現 ANR(Android is Not Responding)
Life cycle 最重要 , 要記熟
15
Activity life cycle 啟動出現畫面
onCreate()onStart()onResume()
關閉畫面結束onPause()onStop()onDestroy()
16
Activity life cycleMethod Description Killable
?Next
onCreate()
Activity 建立時被呼叫必要進行項目:設定 view & 將資料與view 中的 list bind
之後一定是 onStart()
No onStart()
onRestart()
如果此 Activity 已經 stopped, 重新啟動即被呼叫之後一定是 onStart()
No onStart()
17
Activity life cycleMethod Description Killable
?Next
onStart() Activity準備要被 user 看到時被呼叫之後 ,
若要顯示 onResume()
若隱藏 onStop()
No onResume()
onStop()
onResume()
將要與 user 互動 , 本 Activity 已經在Activity Stack 最上層
No onPause()
18
Activity life cycleMethod Description Killable
?Next
onPause() 當即將移動到另一個 Activity 時執行通常在此處 [ 記錄重要參數 ][暫停動畫 ][停止耗 CPU工作 ]
回到最前端 onResume()
User 即將看不到 onStop()
Yes onResume()
onStop()
onStop() 不再被 user 看到 , 可能是啟動了新的Activity,或者舊的 Activity 被拉到前面 , 或者本 Activity 要被刪除再次與 user 互動 onRestart()
要刪除 onDestroy()
Yes onRestart()
onDestroy()
19
Activity life cycleMethod Description Killable
?Next
onDestroy() 這 Activity 已經被 finish()
因為系統資源太少 , 由系統主動刪除Yes 無
20
Service長時間於背景執行 , 無 UI 畫面 可避免 ANR 的問題 例
播放音樂背景接收網路訊息壓縮 or解壓縮檔案
利用 bindService(), 讓 Activity 與 Service溝通
Service Life Cycle 1 與 Activity 相似 , 有些許差異 onCreate and onStart differences
Services can be started when a client calls the Context.startService(Intent) method. If the service isn't already running, Android starts it and calls its onCreate method followed by the onStart method. If the service is already running, its onStart method is invoked again with the new intent. So it's quite possible and normal for a service's onStart method to be called repeatedly in a single run of the service.
21
Service Life Cycle 2 onResume, onPause, and onStop are not
neededRecall that a service generally has no user
interface, so there isn't any need for the onPause, onResume, or onStop methods. Whenever a service is running, it is always in the background.
22
Service Life Cycle 3 onBind
If a client needs a persistent connection to a service, it can call the Context.bindService method. This creates the service if it is not running, and calls onCreate but not onStart. Instead, the onBind method is called with the client's intent, and it returns an IBind object that the client can use to make further calls to the service. It's quite normal for a service to have clients starting it and clients bound to it at the same time.
23
Service Life Cycle 4 onDestroy
As with an activity, the onDestroy method is called when the service is about to be terminated. Android will terminate a service when there are no more clients starting or bound to it. As with activities, Android may also terminate a service when memory is getting low. If that happens, Android will attempt to restart the service when the memory pressure passes, so if your service needs to store persistent information for that restart, it's best to do so in the onStart method.
24
25
Broadcast Receiver 監聽有興趣的 Intent 利用 sendBroadcast() 啟動一個廣播 , 並附上對
應參數 要於 5sec. 內完成 可啟動特定 Activity or Service or 更新畫面 例
Android 系統於電力狀況有變動時 , 傳遞一個電力廣播 若我們是一個電力容量顯示程式 , 則可接收此廣播 ,
並顯示於畫面上 , 或鈴聲 , 或震動 連結
26
Content Provider 與其他程式分享資料 例
存取 Contactcontent://contacts/people/45 (傳回 : 聯絡人編號
45 的聯絡人記錄 )content://contacts/people/ (傳回 : 全部聯絡人 )
連結連結 2
27
Setup EnvironmentPreparing stuffPDA Driver setupAndroid SDK DirectoryGoogle Market
28
Preparing stuff JDK 6 Eclipse 3.5.1 Android SDK ADT
AndroidDeveloperTools
設定 eclipse Menu/
Window/Preference
選定 Android sdk root dir
29
設定 eclipse, 完成新增專案 , 可
以選擇 Android project 即可
30
PDA driver 安裝 Usb 與 pda連線
31
PDA driver 安裝挑選 sdk 的 usb_driver 目錄
32
PDA driver 安裝 安裝中
33
PDA driver 安裝 完成 , 可能需要重開機
34
PDA連線設定 Settings Application Settings Development 三個項目要打勾
35
可將程式執行於 PDA 完成連線 , 且可以執行於 PDA
36
Android SDK Directory Add-ons : 外加元件 Docs : sdk文件 Plateforms:各版 sdk Tools : sdk 工具 Usb_driver : 手機連 usb工具
37
38
Market 1 建立帳號
39
Market 2 繳費 $25
40
Market 3 可上傳自己的專案
41
Market 4 從 pda market 下載
42
Let’s starting codingAndroid 專案 : HelloCSAndroidManifest.xmlUI Layout xmlU2ex architectureReference
Android 專案 : HelloCS
43
Android 專案 : HelloCS
專案目錄展開
44
Android 專案 : HelloCS 執行
45
Android 專案 : HelloCS 啟動 啟動 simulator
46
Android 專案 : HelloCS 完成 執行結果
47
48
AndroidManifest.xml 定義此專案使用的 Activity 與其屬性
UI Layout main.xml
49
xml 編輯器
WYSWYG 編輯器可增加 view more easy
50
U2ex architecture
Application Server
WPS Server
Android PDA
1. Wifi Scan
1 Wifi signal1 Wifi signal
1 Wifi signal
2. Send log to WPS Server
4. Send to Server
3. Calculate position
5. Send AD to PDA
51
Reference Google Android SDK 開發範例大全 ( 書 ) 史丹利部落格 Google developers 癮科技 ysl 的程式天堂 Eoe Anddev.org 分享程式 source(google)
52
Ref : Android source 所有 Android source
http://www.kandroid.org/android_pdk/index.html