嵌入式系統for Android

Download Report

Transcript 嵌入式系統for Android

嵌入式系統與
Android系統
Embedded System &
Android
楊昌益
Android核心編譯
Ubuntu linux ver.10 HD space >16GB
Android系統需三個系統檔
–
–
–
2015/4/9
u-boot
Linux核心zImage
Android系統檔
2

安裝Toolchain
– $mkdir linux_system
– $cd linux_system
– Copy CD之/Android1.6/cross_compiler/4.3.1-
eabi-armv6-20080707.tar.bz2
– $tar jxvf 4.3.1-eabi-armv6-20080707.tar.bz2
– $su
– input password
2015/4/9
3
– #mkdir /usr/local/arm
– #cp –rf 4.3.1-eabi-armv6 /usr/local/arm/
– #exit
– $export CCACHE_PATH=/usr/local/arm/4.3.1-eabi-
armv6/usr/bin-ccache:$CCACHE_PATH (can put it into .bashrc to
accelerate compiling)
– export PATH=$PATH:/usr/local/arm/4.3.1-eabiarmv6/usr/bin:/usr/local/arm/4.3.1-eabi-armv6/bin
– export
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/arm/4.
3.1-eabi-armv6/mpfr/lib:/usr/local/arm/4.3.1-eabiarmv6/gmp/lib:/usr/local/arm/4.3.1-eabi-armv6/lib
2015/4/9
4

安裝Linux kernel
– Copy CD之/Android1.6/kernel/dma6410xp-
linux-2.6.27_100205.tar.bz2 to ~/linux_system
– $tar jxvf dma6410xp-linux-2.6.27100205.tar.bz2
– $cd dma6410xp-linux-2.6.27
2015/4/9
5
– $make dmatek6410_android_defconfig (to
produce a new .config for platform)
– $make clean
– $make zImage (a new zImage
in ./arch/arm/boot)
2015/4/9
6

Update Java tools
– $sudo apt-get update (“Enter”, input password)
– $sudo apt-get install sun-java6-jdk (ask “確定”,
–
–
–
–
–
2015/4/9
press TAB+SPACE
$sudo apt-get install flex
$sudo apt-get install bison
$sudo apt-get install gperf
$sudo apt-get install libsdl-dev
$sudo apt-get install libesd0-dev
7
– $sudo apt-get install libwxgtk2.6-dev
– $sudo apt-get install zlib1g-dev
– $sudo apt-get install build-essential
– $sudo apt-get install libncurses5-dev
(or sudo ./dmatek_ubuntu.sh)
2015/4/9
8

安裝Android file system
– Copy CD之
/Android1.6/Android/dma6410xp_android_sour
ce_100205.tar.bz2 to ~/linux_system
– $tar jxvf
dma6410xp_android_source_100205.tar.bz2
– $cd dma6410xp_android_source
2015/4/9
9
– Open .bashrc to add
 export SEC_PRODUCT=generic
 export
SEC_OUTDIR=~/linux_system/dma6410xp_androi
d_source/android_source/filesystem
 export
PATH=~/linux_system/dma6410xp_android_source
/script:$PATH
– $source ~/.bashrc
2015/4/9
10
– $cd
linux_system/dma6410xp_android_source/andr
oid_source
– $build_android.sh (needs about 2 hours)
– $ls –al filesystem/ (you can find ramdiskuboot.img system.img and userdata.img 3 files)
2015/4/9
11
– $cd ../busybox (Android includes only the basic
–
–
–
linux instructments. Busybox can be inserted if
you need the whole linux instructments)
$cp –af
_install/bin/busybox ../android_source/out/targe
t/product/generic/system/bin
$cd ../android_source
$build_pre.sh
$build_android.sh
2015/4/9
12
–
Burn-in image files

Burn-in linux kernel and Android file
– Connect platform with PC by using UART0
– Open ASCII Terminal in Windows PC and set
115200,N,8,1,No flow control.
– Power on platform and hit the space in
Terminal
2015/4/9
13
2015/4/9
14
– Check your PC’s IP address
– Type “pri” in ASCII Terminal to check ipaddr
& serverip
– Type “setenv ipaddr xx.xx.xx.xx” to set your
own IP
– Type “setenv serverip yy.yy.yy.yy” to set tftp
server ip
– Type “saveenv” to save information
2015/4/9
15
– Run tftpd32.exe in your PC
– Set correct directory
2015/4/9
16
– Connect platform with PC by using Ethernet
– In ASCII Terminal, type “tftp 0x50008000
zImage;nand erase 0x600000 0x300000;nand
write 0x50008000 0x600000 0x300000
– Type “tftp 0x50008000 ramdisk-uboot.img;
nand erase 0x900000 0x100000;nand write
0x50008000 0x900000 0x100000
2015/4/9
17
– Type “tftp 0x50008000 system.img; nand erase
0xa00000 0x4300000;nand write.yaffs
0x50008000 0xa00000 0x{filesize}
– Type “tftp 0x50008000 userdata.img; nand
erase 0x6100000 0x1f00000;nand write.yaffs
0x50008000 0x6100000 0x840
2015/4/9
18
How to write an Android program
Hello World
Execute “eclipse.exe” in Windows PC
 Open new project

2015/4/9
19
2015/4/9
20
2015/4/9
21
2015/4/9
22
“File”->”Restart” (error can be cancelled)
 Press right-hand button on the mouse in the
“Hello World”project

2015/4/9
23
2015/4/9
24
2015/4/9
25

Some features on Eclipse
2015/4/9
26
2015/4/9
27
2015/4/9
28
2015/4/9
29
2015/4/9
30
程式中加除錯訊息
透過「Log」函式,在程式碼中加入一個
自訂的「記錄點」或「檢查點」
 「LogCat」工具查看記錄

2015/4/9
31
android.util.Log

Log.代號(String tag, String msg);
 Log.v (VERBOSE) :詳細訊息
 Log.d (DEBUG) :除錯訊息
 Log.i (INFO):通知訊息
 Log.w (WARN): 警告訊息
 Log.e (ERROR) :錯誤訊息

Ex:
 Log.i(“Hello_info”, “測試除錯訊息”)
2015/4/9
32
2015/4/9
33
Eclipse快速鍵

自動補齊函式名稱
 Alt+/

將程式碼變成註解
 Ctrl+/

自動import類別
 Ctrl+Shift+o

函式或變數自動更正
 Ctrl+1
2015/4/9
34
程式運作模型
Java
Code
Res
files
R.java
編譯程式
Android Package
.apk
安裝程式
2015/4/9
Task / Process
(Application)
35
程式目錄架構
2015/4/9
36
Activity生命週期

應用程式存在與否不是由應用程式本身
所自行決定,而是由Android系統透過運
行機制決定
2015/4/9
37
2015/4/9
38









public void onCreate(){ }
public void onStart(){ }
public void onResume(){ }
public void onPause(){ }
public void onStop(){ }
public void onRestart(){ }
public void onDestroy(){ }
Activity 生命週期由「Create」狀態開始,由
「Destroy」狀態結束
Create時分配資源,銷毀(Destroy)時釋放資源
2015/4/9
39
android.app.Activity

View findViewById(int id)
 找出指定資源id的元件





final CharSequence getTitle()
final int getTitleColor()
void setTitle(int titleId) / void setTitle(CharSequence title)
void setTitleColor(int textColor)
void setContentView(int layoutResID)
 將layout資源設定呈現在Activity內容中

void finish() :結束Activity
2015/4/9
40
XML資源文件

strings.xml
2015/4/9
41
版本
編碼方式

<?xml version="1.0" encoding="utf-8"?>
 <resources>
資源檔的開始

<string name="hello">Hello World,
hello!</string>
字串標籤
字串名稱
字串內容

<string
name="app_name">HelloWorld</string>

<string name="test">字串測試</string>
資源檔的結束
 </resources>
2015/4/9
42
文字資源使用方法

Java
 CharSequence
str_var1=getResources().getString(R.string.hell
o);
 setTitle(str_var1);

XML
 android:text="@string/hello"
引用文字字串
2015/4/9
字串名稱
43
文字顏色 <color>







<color name=“red">#FF0000</color>
R:紅色 G:綠色 B:藍色 A:透明度(0:透明 FF不透
明[預設])
16進位表示
#RGB
#ARGB
#RRGGBB
#AARRGGBB
2015/4/9
44
文字顏色資源使用方法

Java
 int r = getResources().getColor(R.color.red);
 setTitleColor(r);

XML
 android:textColor="@color/red"
2015/4/9
45
繪圖元件顏色<drawable>

<drawable name="red">#F00</drawable>
 Java
 Drawable red
=getResources().getDrawable(R.drawable.red);

XML
 android:background="@drawable/red"
 android:foreground="@drawable/green"
2015/4/9
46
尺寸<dimen>

<dimen name="ten_pixel">10px</dimen>
 px : 像素
 In : 英吋
 mm : 公釐
 pt : 一點為1/72英吋
 dp/dip : 160dpi螢幕的一個像素
 sp : 隨螢幕尺寸改變的一個像素
2015/4/9
47
尺寸資源使用方法

Java
 float
dim=getResources().getDimension(R.dimen.ten
_pixel);
 XML
 android:textSize="@dimen/ten_pixel"
2015/4/9
48
<style>










<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="myStyle">
<item name="android:color">@color/r</item>
<item name="android:foreground">@drawable/blue</item>
<item name="android:background">@drawable/green</item>
<item name="android:textColor">@color/g</item>
<item name="android:textSize">@dimen/ten_pixel</item>
</style>
</resources>
2015/4/9
49
style資源使用方法

Java
 setTheme(R.style.myStyle);
 XML
 android:theme="@style/myStyle"
2015/4/9
50
元件佈局

main.xml
2015/4/9
51
2015/4/9
52
2015/4/9
53
佈局方式




LinearLayout
RelativeLayout
TableLayout
FrameLayout
2015/4/9
54
LinearLayout



線性佈局
標籤:< LinearLayout >,可多層LinearLayout
屬性
 android:gravity (元件對齊方式,多個值用|隔開)
 top, bottom, left, right, center_vertical
fill_vertical ,center_horizontal ,fill_horizontal
center ,fill
2015/4/9
55
LinearLayout

屬性
 android:orientation (線性方式)
 horizontal
 vertical
 android:layout_width (寬度)
 fill_parent
 wrap_content
 指定大小
 android:layout_heigh (高度)
2015/4/9
56
邊界屬性
 android:layout_marginBottom
元件與底部邊線的距離
 android:layout_marginLeft
 元件與左邊線的距離
 android:layout_marginRight
 元件與右邊線的距離
 android:layout_marginTop
 元件與上邊線的距離

2015/4/9
57
padding屬性
 android:padding

設定四個邊框與元件或文字的距離
 android:paddingBottom

設定底部邊框與元件或文字的距離
 android:paddingLeft

LinearLayout
設定左邊框與元件或文字的距離
margin
 android:paddingRight

設定右邊框與元件或文字的距離
 android:paddingTop

2015/4/9
Button
padding
設定上邊框與元件或文字的距離
58
How to use the LinearLayout to
finish the following GUI?
2015/4/9
59
Method 1

First step
2015/4/9
60

Modify xml file
2015/4/9
61
Method 2
2015/4/9
62
2015/4/9
63
2015/4/9
64
2015/4/9
65
2015/4/9
66
2015/4/9
67
RelativeLayout


相對佈局:可指定元件或佈局對齊任一個元件或佈局
 標籤:<RelativeLayout>
屬性
 android:layout_above:將此元件放置在指定ID元件之上
 android:layout_above="@id/edit"
 android:layout_alignBaseline:將此元件的基準線放置在與指定ID元件
相同基準線上
 android:layout_alignBottom:底部基線
 android:layout_alignLeft:左邊線
 android:layout_alignParentBottom

true:底部基線與主佈局底部基線對齊
 android:layout_alignParentLeft
 true:左邊線與主佈局左邊線對齊
2015/4/9
68
RelativeLayout

屬性
 android:layout_alignParentRight

true:右邊線與主佈局右邊線對齊
 android:layout_alignParentTop

true:上邊線與主佈局上邊線對齊
 android:layout_alignRight :將此元件的右邊線放置在與指定ID
元件相同右邊線上
 android:layout_alignTop:上邊線
 android:layout_below :將此元件的上邊線放置在指定ID元件之
下
 android:layout_centerHorizontal

2015/4/9
true:將此元件放置於於主佈局的水平置中
69
RelativeLayout

屬性
 android:layout_centerInParent
 true:將此元件放置於於主佈局的垂直和水平置中
 android:layout_centerVertical
 true:將此元件放置於於主佈局的垂直置中
 android:layout_toLeftOf :將此元件的右邊線放置在與
指定ID元件的左邊
 android:layout_toRightOf :將此元件的左邊線放置在
與指定ID元件的右邊
2015/4/9
70
其它重要屬性







android:layout_marginBottom
android:layout_marginLeft
android:layout_marginRight
android:layout_marginTop
android:layout_width
android:layout_heigh
…
2015/4/9
71
How to use RelativeLayout to
finish the following GUI
2015/4/9
72
2015/4/9
73
2015/4/9
74
元件
2015/4/9
75
View
TextView
EditText
ToggleButton
ViewGroup
Button
CheckBox
ImageView
RadioButton
ImageButton
ViewGroup
AdapterView
ListView
Spinner
2015/4/9
FrameLayout
LinearLayout
Gallery
TableLayout
RelativeLayout
76 76

Android API提供的元件
 TextView
 Button
 ToggleButton
 ImageButton
 RadioButton
 CheckBox
…
2015/4/9
77
TextView



文字元件,顯示文字
標籤:< TextView>
屬性
 android:autoLink:讓文字上的連結自動變成可點擊的連結
 none
 web
 email
 phone
 map
 all
2015/4/9
78
TextView

屬性
 android:gravity:文字對齊方式
 android:height
 android:width
 android:typeface:字體




normal
sans
serif
monospace
 android:textStyle
 normal
 bold
 italic
2015/4/9
79
TextView

屬性
 android:textSize :文字大小
 android:textColorLink :連結顏色
 android:text :顯示的文字
 android:textColorHighlight :選取文字的顏色
 android:textColor :文字顏色
 android:singleLine :單行或多行 (true false)
android:password: (true false)
 android:phoneNumber: 電話號碼 (true false)
2015/4/9
80
TextView

屬性
 android:numeric
 integer
 signed
 decimal
 android:lines
 android:hint :當Text是空的時候,就會顯示hint中的文字
 android:textColorHint
 android:digits:除接受數字外,允許此欄位設定的字元
 跳脫字元需使用'\\;'
 android:digits="1234567890.+-*/%\\n()"
2015/4/9
81
EditText
可輸入文字元件
 標籤:< EditText>
 繼承TextView,其所具有屬性請參考
TextView

2015/4/9
82
Button
按鈕
 標籤:< Button>
 繼承TextView,其所具有屬性請參考
TextView

2015/4/9
83
ToggleButton
on / off 按鈕
 標籤:<ToggleButton>
 屬性

 android:textOn

開啟時所顯示的文字
 android:textOff

關閉時所顯示的文字
 android:checked

2015/4/9
初始時是選取或未選取 (true, false)
84
RadioButton
單選按鈕
 標籤:< RadioButton >
 多選一時,需在多選的RadioButton標籤
外,加上<RadioGroup>

 RadioGroup屬性請參考佈局屬性

android:orientation
– 垂直或水平排列
2015/4/9
85
CheckBox
複選按鈕
 標籤:< CheckBox >
 屬性請參考TextView
 屬性

 android:checked

2015/4/9
初始時是選取或未選取 (true, false)
86
ImageButton

圖片按鈕

標籤:< ImageButton >

屬性
 android:src

按鈕上的圖片或色彩
 android:adjustViewBounds

調整圖片大小以符合按鈕大小
 android:cropToPadding
 剪裁圖片配合按鈕大小
 android:scaleType
 控制圖片大小或位置

2015/4/9
matrix、fitXY、fitStart、fitCenter、fitEnd、center、centerCrop、
centerInside
87
DatePicker
年月日元件
 標籤:< DatePicker>
 屬性

 android:startYear
 可選起始年份
 android:endYear
 可選結束年份
2015/4/9
88
TimePicker
時間挑選元件
 標籤:< TimePicker>

2015/4/9
89
ListView
條列式選單
 標籤:< ListView>
 屬性
 android:choiceMode
 選擇模式

– none 正常條列式選單
– multipleChoice 多選
– singleChoice 單選
 android:entries="@array/data“
選單中的選項,引用陣列
 android:divider
 分隔線顏色
 android:dividerHeight:分隔線高度

2015/4/9
90
Spinner
下拉式選單
 標籤:< Spinner>

<Spinner
android:id="@+id/S1"
android:layout_height="wrap_
content"
android:layout_width="fill_par
ent"
android:entries="@array/data"
/>
2015/4/9
91
private static final String[] data=
new String[]{“5”, “2”, “3”,
“4”};
Spinner sp=(Spinner)findViewById(R.id.S1);
sp.setAdapter(new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item,data));
2015/4/9
92
練習 ─ 排版

請排版成右圖
性別:
男
女
Radio button
身高:
EditText
Spinner包含公尺和公分
計算
Button
2015/4/9
TextView
93
事件
2015/4/9
94
View 介面-- OnClickListener

設定元件觸發事件
Button bt=(Button)findViewById(R.id.btn1);
bt.setOnClickListener(this);
this,表示要在目前這個class(程式)中,實作事件介面
程式需實作OnClickListener
 implements OnClickListener
 程式中需實作函式onClick(View v)

public void onClick(View v) {
…
}
 當按下元件時會執行onClick函式
 View v表示觸發這個事件的元件
2015/4/9
95

Example:
– Layout:
2015/4/9
96
2015/4/9
97

Source java
2015/4/9
98
2015/4/9
99
練習 ─ 標準體重計算

根據以下公式計算標準體
重
 男性的標準體重 =
[ 身高 ( 公分 ) - 80 ] × 0.7
 女性的標準體重 =
[ 身高 ( 公分 ) - 70 ] × 0.6
 體重比標準體重多百分之十或少百分
之十以內均屬於正常範圍
Radio button
性別:
男
女
Spinner包含公尺和公分
身高:
EditText
計算
Button
顯示標準體重範圍
2015/4/9
TextView
100
2015/4/9
101

Some example for source code
2015/4/9
102
Installation
2015/4/9
103
Connecting mobile first time

HTC Hero as example
– Connect HTC using USB cable
– From “控制台” ->”新增硬體精靈” in
windows XP
2015/4/9
104
2015/4/9
105
2015/4/9
106
2015/4/9
107
Installation
executing “ddms.bat” from “android-sdkwindows”
 In HTC, “設定”->”應用程式”->”未知的
來源”設定成”ˇ”
 In HTC, “設定”->”應用程式”->”開發”
選”USB除錯中”和”保持喚醒”設定
成”ˇ”
 In windows XP, please install the following
package if your PC connects HTC first time.

2015/4/9
108

Check USB device in HTC has been “掛載”
– (在”手機狀態欄”下拉選單)
Check “HTC Sync\HTCSync” file in
microSD card
 Execute HTCSync

2015/4/9
109
Install application

In Eclipse, “run as” or “debug as” android
application and select your device (emulator
or mobile station).
2015/4/9
110
APK

In Eclipse
2015/4/9
111
2015/4/9
112
2015/4/9
113
2015/4/9
114
2015/4/9
115
2015/4/9
116
2015/4/9
117