Download Android application source layout

Document related concepts
no text concepts found
Transcript
Android多核心嵌入式多媒體系統設計與實作
Android Application Development
賴槿峰 (Chin-Feng Lai)
Assistant Professor, institute of CSIE, National Ilan University
Nov. 10th 2011
© 2011 MMN Lab. All Rights Reserved
2011 資訊軟體技術人才培訓
Outline
•
•
•
•
•
•
2
Android Application Layer
Dalvik Virtual Machine
Android Development Environment
Android application source layout
Android Debug Tools
LAB : Program & Run Android apk on devkit8000
•
•
•
•
•
•
3
Android Application Layer
Dalvik Virtual Machine
Android Development Environment
Android application source layout
Android Debug Tools
LAB : Program & Run Android apk on devkit8000
Android Application Layer
4
Android Application Layer
• Activity manager
– Manage the life cycle of applications
• Content Provider
– Share data between applications
• Service
– A Service is an application component that can perform long-running
operations in the background and does not provide a user interface
• Broadcast Intent Receiver
– if the service does not also provide binding, the intent delivered
with startService() is the only mode of communication between the
application component and the service
5
Android Application Layer
• Activity manager
– Manage the life cycle of applications
– Activity Lifecycle
• Activities in the system are managed as an activity stack.
• When a new activity is started, it is placed on the top of the stack
and becomes the running activity
• Developers have to care about the implementation of each stage of
lifecycle
6
Android Application Layer
• Activity.class:
public class Activity extends ApplicationContext {
protected void onCreate(Bundle savedInstanceState);
protected void onStart();
protected void onRestart();
protected void onResume();
protected void onPause();
protected void onStop();
}
7
protected void onDestroy();
Activity Lifecycle
Android Application Layer
• Activity Stack
Launch Activity 1
(By Home Launcher)
Activity1
onStart
Launch Activity 2
(By Activity 1)
onStart
onPause
Activity2
Activity2
Activity1
Activity1
onPause
onRestart
onStop
onDestory
Activity2
Activity2
Activity1
Activity1
onStart
running
Back to Home
(Back button on Activity 1)
8
Back to Activity 1
(Back button on Activity 2)
Activity1
onStop
onDestory
Android Application Layer
• Content Provider
• Content providers store and retrieve data and make it accessible
to all applications.
• The only way to share data across applications in android
• The accessible data like:
• Contact , phone number , call logs , dictionary , … etc
9
Android Application Layer
• Content Provider
Content://com.android.example/appname/#<id>
ContentProvider
ContentResolver
Activity
Services
Query()
Insert()
Delete()
Update()
………..
./data/data/….
………….
………….
AndroidManifest.xml
<provider android:name=".example "
android:authorities="com.android.example"
... >
10
Network
DataBase
(sqlite3)
File
•
•
•
•
•
•
11
Android Application Layer
Dalvik Virtual Machine
Android Development Environment
Android application source layout
Android Debug Tools
LAB : Program & Run Android apk on devkit8000
Dalvik Virtual Machine
• Since the development language of Android application
is Java. Need a Java virtual machine for running
application.
• Content in Android Runtime
•
•
Core Libraries
Dalvik Virtual Machine
–
–
12
Compile Java code to Dalvik Excutable (dex format)
Run in dalvik VM
Dalvik Virtual Machine
• Register-based virtual machine for efficient execution,
not stack-based in traditional Java
• Managed by zygote , using fork() in linux kernel to create
• Each application has its own virtual machine for running
Compare traditional stack-based JVM with register-based JVM
13
Before interpret
Stack-based
Register-based
A=B+C
ILOAD C
ILOAD B
IADD
ISTORE A
IADD A B C
Dalvik Virtual Machine
• Advantages of register-based JVM
–
–
–
–
–
14
Reducing the data movement(constants stored in register)
Reducing the number of instructions
Reducing memory access times
More real-time reactions
More easier to implement optimization on computation than
stack-based JVM
Dalvik Virtual Machine
• Android Application Program Flow
AndroidManifest.xml
R.java
Resource
res/*
Java souce
src/*
Java
Compiler
Dex
Converter
Xml resource compilation
+
Other resource pre-process
Java Class
android.jar
Dalvik dex
ApK
Builder
Android APK
15
Dalvik Virtual Machine
Jar to Dex
16
Dalvik Virtual Machine
• Zygote Fork
Core library
Dex file
APP
dex
Zygote
Heap
Heap
Structure
Daemons
•usdb
•adbd
•debuggerd
•rlid
Heap
Structure
zygote
Service
Manager
Fork()
runtime
Zygote
Zygote
Heap
Dex
Structure
zygote
Fork()
Core library
Dex file
init
Zygote
Heap
Binder Driver
Dex
Structure
Kernel
17
Dex
Structure
APP
dex
Core library
Dex file
zygote
•
•
•
•
•
•
18
Android Application Layer
Dalvik Virtual Machine
Android Development Environment
Android application source layout
Android Debug Tools
LAB : Program & Run Android apk on devkit8000
Android Development Environment
• Operating System
– Windows
– Linux
– Mac OS X
19
Android Development Environment
• Development tools
–
–
–
–
20
Java JDK5 or JDK6
Eclipse IDE
Eclipse ADT
Android SDK
Using emulator to debug first
Android Development Environment
• Android Emulator
– Included in Android SDK
– Using QEMU emulator
• QEMU is a generic and open source machine emulator and
virtualizer.
• QEMU can run OSes and programs made for one machine (e.g. an
ARM board) on a different machine (e.g. your own PC).
• Emulate the hardware and software behaviors
21
Android Development Environment
• Android Emulator
– Hardwares emulate:
•
•
•
•
•
•
•
22
ARM926ej-S CPU
MMC
Keyboard
USB
Framebuffer
TTY driver
NAND FLASH
Android Development Environment
• Development tools on Linux Ubuntu:
– Install java jdk
• sudo apt-get install sun-java5-jdk
– Install Android SDK
• http://dl.google.com/android/installer_r20.0.3-windows.exe
23
Android Development Environment
• Development tools on Linux Ubuntu:
– Install eclipse IDE
• http://www.eclipse.org/downloads/
– Install eclipse ADT(Android Development Tools)
• Using software update in eclipse and type:
http://developer.android.com/intl/zh-TW/tools/sdk/eclipse-adt.html
24
Android Development Environment
• Install ADT in eclipse
25
Android Development Environment
• Set the Android SDK path in eclipse
– Tool bar>>Windows>>Preferences>>Android
26
Android Development Environment
• AVD
– Windows>>Android SDK and AVD Manager
27
Android Development Environment
• In eclipse, File >> New >> Android Project
• And start your Android trip!
28
•
•
•
•
•
•
29
Android Application Layer
Dalvik Virtual Machine
Android Development Environment
Android application source layout
Android Debug Tools
LAB : Program & Run Android apk on devkit8000
Android application source layout
• What do we have to concern about an android project ?
–
–
–
–
Java class (src)
xml of Android layout (res)
Jar of Android version (android sdk)
AndroidManifest.xml
• Permissions of activities
• Services in application
30
Android application source layout
Java code
SDK version
Layout file
Activity definition
31
Android application source layout
• AndroidManifest.xml
– The manifest presents essential information about the application to the
Android system
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=http://schemas.android.com/apk/res/android
package="com.demo.android.mmn“
android:versionCode="1“
android:versionName="1.0">
<application android:icon="@drawable/icon">
<activity android:name=".mmn" 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>
<uses-sdk android:minSdkVersion="X" />
</manifest>
Useful reference: http://developer.android.com/guide/topics/manifest/manifest-intro.html
32
Android application source layout
• Resource (res)
– drawable, layout, vaule
Layout define the locations of each UI components
There are five layout used in Android
Absolute Layout
Linear Layout
Relative Layout
Frame Layout
Table Layout
33
Android application source layout
• Resource (res) - layout
34
Android application source layout
• Hierarchy Viewer
– must connect your device or launch an emulator
– <sdk>/tools/hierarchyviewer.bat
View Hierarchy
Pixel perfect
view
Current visible
activity
35
Android application source layout
• Hierarchy Viewer
– Pixel perfect view
Pixel perfect
view
36
Android application source layout
• Hierarchy Viewer
– Pixel perfect view
View Hierarchy
37
Android application source layout
• Resource (res) - vaule
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name=“mmn">how are you mad</string>
<string name=“mmn1"> how are you diemno</string>
<string name=“mmn2"> how are you diousk</string>
</resources>
38
Android application source layout
• Gen – generated Java File
– R.java
package com.demo.android.bmi;
public final class R {
public static final class string {
public static final int height=0x7f050000;
public static final int report_back=0x7f050005;
public static final int result=0x7f050003;
public static final int submit=0x7f050002;
public static final int suggest=0x7f050004;
public static final int weight=0x7f050001;
}
}
– Java Source code
view_result.setText(getString(R.string.weight)
39
Android application source layout
• Any View object may have an integer ID associated with it
– The plus-symbol (+) means that this is a new resource name that must
be created and added to our resources
android:id="@+id/my_button"
– There are a number of other ID resources that are offered by the
Android framework. When referencing an Android resource ID, you do
not need the plus-symbol, but must add the android package
namespace
android:id="@android:id/empty"
40
Android application source layout
• Absolute Layout
– Absolute Layout is based on the simple idea of placing each
control at an absolute position.(deprecated!)
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<Button
android:id="@+id/backbutton" android:text="Back"
android:layout_x="10px" android:layout_y="5px" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</AbsoluteLayout>
41
Android application source layout
• FrameLayout
– FrameLayout is designed to display a single item at a time. The
item will be display on the top left of the UI layout by default
<FrameLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:text="Learn-Android.com"
android:textSize="24sp"
android:textColor="#000000"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"/> </FrameLayout>
42
Android application source layout
• Linear Layout
– Linear layout organizes elements along a single line ( vertical or
horizontal).
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/backbutton" android:text="Back"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<TextView android:text="First Name" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
43
Android application source layout
• Linear Layout
android:orientation="horizontal"
android:orientation=“vertical"
44
Android application source layout
• Relative Layout
– Relative Layout lays out elements based on their relationships
with one another by their IDs.
<RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button android:id="@+id/backbutton" android:text="Back"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<TextView android:id="@+id/firstName"
android:text="First Name"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/backbutton" />
</RelativeLayout>
45
Android application source layout
• TableLayout
– Table Layout organizes content into rows and columns. The rows
are defined in the layout XML, and the columns are determined
automatically by Android.
<TableLayout android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow>
<Button android:id="@+id/backbutton" android:text="Back" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </TableRow>
<TableRow>
<TextView android:text="First Name" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_column="1" />
<EditText android:width="100px" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </TableRow>
</TableLayout>
46
•
•
•
•
•
•
47
Android Application Layer
Dalvik Virtual Machine
Android Development Environment
Android application source layout
Android Debug Tools
LAB : Program & Run Android apk on devkit8000
Android Debug Tools
• Android SDK
• There are a set of debug tools in android SDK, Tool is
located on platform-tool and tools directory.
– Android Debug Bridge(ADB)
• a versatile command line tool that lets you communicate with an emulator
instance or connected Android-powered device. It is a client-server program.
– Dalvik Debug Monitor Server (DDMS)
• provides port-forwarding services, screen capture on the device, thread and
heap information on the device, logcat, process, and radio state information
48
Android Debug Tools
• Android Debug Bridge (adb) is a versatile command line
tool that lets you communicate with an
emulator instance or connected Android-powered device.
• adb tool in <sdk>/platform-tools/
– Shell mode
• Opan “cmd” on window
• C:\android-sdk_r11-windows\android-sdk-windows\platform-tools>adb shell
49
Android Debug Tools
•
adb tool in <sdk>/platform-tools/
– Install android application (APK)
• $ adb install <path_to_apk>
• -r (recover)and –f (force)
– communicate PC with android device
• adb pull <remote> <local>
• adb push <local> <remote>
EX:
adb push mmn.txt
adb pull /temp/mmn.txt
/temp/mmn.txt
mmn.txt
– Show some service information
adb shell dumpsys SurfaceFlinger
adb shell dumpsys battery
adb shell dumphys betteryinfo
50
Android Debug Tools
• Android Debug Bridge(ADB) other command
– When android devices connected or emulators started
• $cd <android_SDK_path>/platform-tools
–
–
–
–
–
51
$./adb devices
#list detected devices
$./adb root
#change root Permissions
$./adb remount
#remount system
$./adb kill-server
#end adb
$./adb forward tcp:x tcp:x #port forward
Android Debug Tools
• Dalvik Debug Monitor Server (DDMS)
– DDMS is integrated into Eclipse and is also shipped in
the tools/ directory of the SDK
• From Eclipse: Click Window > Open Perspective > Other... > DDMS.
• From the command line: Type ddms (or ./ddms on Mac/Linux) from
the tools/ directory.
–
–
–
–
–
–
52
Viewing heap usage for a process
Tracking memory allocation of objects
Working with an emulator or device's file system
Examining thread information
Starting method profiling
LogCat information
Android Debug Tools
• Dalvik Debug Monitor Server (DDMS)
Click to start
in eclipse
Applications
run in android
logcat
53
Screen
Capture
Android Debug Tools
• Useful Reference
– http://developer.android.com/guide/index.html
54
•
•
•
•
•
•
55
Android Application Layer
Dalvik Virtual Machine
Android Development Environment
Android Application Layout
Android Debug Tools
LAB : Program & Run Android apk on devkit8000
Lab files
• eclipse_linux.tgz
– Eclipse IDE with ADT for Linux
• android_sdk.tgz
– Android SDK
• mmn_apk.tgz
– Android application to be modified
Step1.Set the Development Environment
• Uncompressing the lab files
• Change to eclipse folder , revise the qq.sh file with correct
path to eclipse binary file
– $cd eclipse/eclipse
– $vim qq.sh (revise as /home/mmn/Downloads/lab9/eclipse/eclipse/eclipse)
– $sh qq.sh (start eclipse)
• Set the workspace path , it will create the folder automatically
Step2. Set the Android SDK path in eclipse
Step3.Import mmn apk source
• Import mmn apk source
– Toolbar >> File >> Import… >> General >>Existing… >> Next
>>Browse
Step3.Import facedetecttest.apk source
Step4.Complete the Apk
• Complete the source code
(eclipse)
– mmn>>src>>mmm.com.tw>>facedetecttest.java
• Create the rest ClickListener function of buttons
• Button : Next、Previous、Exit
Face Detection API
• Face Detection API are defined in SDK
• Class FaceDetector
– findFaces(Bitmap bitmap, Face[] faces)
• bitmap: to be processed (already initiated in the code)
• faces: stored the detected faces (already initiated in the code)
• Class FaceDetector.Face
– float confidence ()
• The confidence factor between 0 and 1
– float eyesDistance ()
– void getMidPoint (PointF point)
– float pose (int euler)
• the Euler axis to retrieve an angle from (EULER_X, EULER_Y or EULER_Z)
Step4.Complete the Apk
• facedetecttest.java
– Set “Next、Previous” button
Step4.Complete the Apk
• facedetecttest.java
– Set button action
Step4.Complete the Apk
• facedetecttest.java
– Find face in the picture
Step4.Complete the Apk
• facedetecttest.java
– Draw the rectangle on face
Step5.Test the apk
• After modification , right click on your project >> Run as
>> android application to test on android emulator
Step6.Run Apk on devkit8000
• In /home/mmn/Downloads/lab9/facedetecttest/bin , you
can find out the facedetecttest.apk .
• Copy the apk file to <android_fs>/system/app/
• Run your apk on devkit8000 and start apk!
– Note: if the apk is updated , you have to restart booting
devkit8000, or the system won’t update it
Related documents