Professional Documents
Culture Documents
Android Developer
Android Developer
江
大
学
软
硬
件
协
同
设
计
Android 课程讲义
实
验
室
http://multicore.zju.edu.cn/fatlab
SmartPhone Application
浙
江
大
学
软
硬
件
协
同
设
计
实
验
室
3、Programming
http://multicore.zju.edu.cn/fatlab
SmartPhone Application
浙
江
大
学
软
硬
件
协
同
设
计
实
验
室
Hello world!
http://multicore.zju.edu.cn/fatlab
2010.06. 浙江大学计算机学院 4
浙
江
大
学
软
HelloAndroid.java
硬
件
协
Java程序
同
设
package com.android.helloAndroid;
计 import android.app.Activity;
实 import android.os.Bundle;
验 public class HelloAndroid extends Activity
室 {
/** Called when the activity is first created. */
http://multicore.zju.edu.cn/fatlab
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
2010.06. 浙江大学计算机学院 5
浙
江
大
学
软
R.java
硬
件
协
Java程序
同 /* AUTO-GENERATED FILE. DO NOT MODIFY.
设 */
计 package com.android.helloAndroid;
实 public final class R {
验 public static final class attr {}
室 public static final class drawable {
public static final int icon=0x7f020000;
http://multicore.zju.edu.cn/fatlab
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
2010.06. 浙江大学计算机学院 6
浙
江
大
学
软
main.xml
硬 Resources
Xml文件
件
协
同
设 <?xml version="1.0" encoding="utf-8"?>
计 <LinearLayout
实 xmlns:android="http://schemas.android.com/apk/res/android"
验 android:orientation="vertical"
室
android:layout_width="fill_parent"
android:layout_height="fill_parent"
http://multicore.zju.edu.cn/fatlab
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
2010.06. 浙江大学计算机学院 7
浙
江
大
学
软
strings.xml
硬
Xml文件
件
协
同 <?xml version="1.0" encoding="utf-8"?>
设 <resources>
计 <string name="hello">Hello Android, 2010.</string>
实 <string name="app_name">HelloAndroid</string>
验 </resources>
室
http://multicore.zju.edu.cn/fatlab
display:
Hello Android, 2010.
2010.06. 浙江大学计算机学院 8
浙
江
大
学
软
RUN
硬 RUN
件
协
同
设
计
实
验
室
http://multicore.zju.edu.cn/fatlab
2010.06. 浙江大学计算机学院 9
浙
江
大
学
软
模拟运行
硬
件
协
同
设
计
实
验
室
http://multicore.zju.edu.cn/fatlab
2010.06. 浙江大学计算机学院 10
浙
江
大
学
软
run
硬
件
协
Start emulator
同 emulator -avd android1.6 -skin HVGA-p
设
计
Link to emulator :
实 telnet localhost 5554
验
室 Make a Phone call:
gsm call 13810000086
http://multicore.zju.edu.cn/fatlab
Send a message:
sms send 13810000086 HelloAndroid
2010.06. 浙江大学计算机学院 11
浙
江
大
学
软
Image display
硬
件
协
add
同 ImageView
设
计
实
验
室
http://multicore.zju.edu.cn/fatlab
2010.06. 浙江大学计算机学院 12
浙
江
大
学
软
Android file type
硬
件
协
Java file----source code
同 android 本身相当一部分都是用java 编写而成
设 android 的应用使用java 来开发。
计
实 Class file----Java class file after compiled
验
Google uses DVM(Dalvik virtual machine).
室
Need change .class to .dex。
http://multicore.zju.edu.cn/fatlab
2010.06. 浙江大学计算机学院 13
浙
江
大
学
软
Apk file
硬
件
协
Apk file----Android上的安装文件
同 An .apk file extension denotes an Android Package (APK)
设 file. This file format, a variant of the JAR format, is used
计 for the distribution and installation of bundled
实
components onto the Android mobile device platform.
验
室 An APK file is an archive that usually contains the
following folders:
http://multicore.zju.edu.cn/fatlab
META-INF
res
and files:
AndroidManifest.xml
classes.dex
resources.arsc
2010.06. 浙江大学计算机学院 14
浙
江
大
学
软
AndroidManifest.xml
硬
件
协
This view shows the actual XML file that results
同 from all the choices made in the other views.
设
计
实
验
室
http://multicore.zju.edu.cn/fatlab
2010.06. 浙江大学计算机学院 15
浙
江
大
学
软
一个AndroidManifest.xml文件的例子
硬
件 <?xml version="1.0" encoding="utf-8"?>
协 <manifest
同 xmlns:android="http://schemas.android.com/apk/res/andr
设 oid" package="com.my_domain.app.helloactivity">
计 <application android:label="@string/app_name">
实
<activity class=".HelloActivity">
验
室 <intent-filter>
<action
http://multicore.zju.edu.cn/fatlab
android:value="android.intent.action.MAIN"/>
<category
android:value="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
2010.06. 浙江大学计算机学院 16
浙
江
大
学
软
Adroid应用
硬
件
协
同
设
计
实
验
室
http://multicore.zju.edu.cn/fatlab
2010.06. 浙江大学计算机学院 17
浙
江
大
学
软
Activities
硬
件
协
Activities are pieces of executable code that come
同 and go in time, instantiated by either the user or
设 the operating system and running as long as they
计
实 are needed. They can interact with the user and
验 request data or services from other activities or
室
services via queries or Intents (discussed in a
http://multicore.zju.edu.cn/fatlab
moment).
Activities usually correspond to display screens:
each Activity shows one screen to the user. When
it is not actively running, an Activity can be killed
by the operating system to conserve memory.
2010.06. 浙江大学计算机学院 18
浙
江
大
学
软
Services
硬
件
协
These are analogous to services or daemons in
同 desktop and server operating systems. They are
设 executable pieces of code that usually run in the
计
实 background from the time of their instantiation
验 until the mobile handset is shut down. They
室
generally don't expose a user interface.
http://multicore.zju.edu.cn/fatlab
2010.06. 浙江大学计算机学院 20
浙
江
大
学
软
Intent Receivers
硬
件
协
The requesting (client) activity issues an Intent,
同 leaving it up to the Android framework to figure
设 out which application should receive and act on it.
计
实 Intents are one of the key architectural elements
验
室
in Android that facilitate the creation of new
applications from existing applications. You will
http://multicore.zju.edu.cn/fatlab
2010.06. 浙江大学计算机学院 21
浙
江
大
学
软
Content providers
硬
件 These are created to share data with other activities or
协
services. A content provider uses a standard interface in
同
设 the form of a URI to fulfill requests for data from other
计 applications that may not even know which content
实 provider they are using.
验
The operating system looks to see which applications have
室
registered themselves as content providers for the given
http://multicore.zju.edu.cn/fatlab
2010.06. 浙江大学计算机学院 22
浙
江
大
学
软
硬
件
协
An application doesn't have to use all of the
同 Android components, but a well-written
设 application will make use of the mechanisms
计
实 provided, rather than reinventing functionality or
验 hardcoding references to other applications.
室
http://multicore.zju.edu.cn/fatlab
2010.06. 浙江大学计算机学院 23
浙
江
大
学
软
Activity
硬
件
协
Android is designed around the unique
同 requirements of mobile applications. In particular,
设 Android recognizes that resources (memory and
计
实 battery, for example) are limited on most mobile
验 devices, and provides mechanisms to conserve
室
those resources.
http://multicore.zju.edu.cn/fatlab
2010.06. 浙江大学计算机学院 24
浙
江
大
学
软
Activity状态管理
硬
件
协
同
设
计
实
验
室
http://multicore.zju.edu.cn/fatlab
2010.06. 浙江大学计算机学院 25
浙
江
大
学
软
Activity Lifecycle
硬
件
协
同
设
计
实
验
室
http://multicore.zju.edu.cn/fatlab
2010.06. 浙江大学计算机学院 26
浙
江
大
学
软
硬
件 An activity monitors and reacts to these events by
协 instantiating methods that override the Activity class
同 methods for each event:
设 onCreate
计 Called when an activity is first created. This is the place you
实 normally create your views, open any persistent datafiles your
验 activity needs to use, and in general initialize your activity.
室 When calling onCreate, the Android framework is passed a
Bundle object that contains any activity state saved from when
http://multicore.zju.edu.cn/fatlab
2010.06. 浙江大学计算机学院 27
浙
江
大
学
软
硬
件
协
onResume
同 Called right after onStart if your activity is the
设 foreground activity on the screen. At this point your
计 activity is running and interacting with the user. You are
实
receiving keyboard and touch inputs, and the screen is
验
室 displaying your user interface.
onResume is also called if your activity loses the
http://multicore.zju.edu.cn/fatlab
2010.06. 浙江大学计算机学院 28
浙
江
大
学
软
硬
件 onPause
协
同 Called when Android is just about to resume a different
设 activity, giving that activity the foreground. At this point
计 your activity will no longer have access to the screen, so
实 you should stop doing things that consume battery and
验 CPU cycles unnecessarily.
室 If you are running an animation, no one is going to be
able to see it, so you might as well suspend it until you
http://multicore.zju.edu.cn/fatlab
2010.06. 浙江大学计算机学院 29
浙
江
大
学
软
硬
件
协
onStop
同 Called when your activity is no longer visible, either
设 because another activity has taken the foreground or
计 because your activity is being destroyed.
实
验 onDestroy
室
The last chance for your activity to do any processing
before it is destroyed. Normally you'd get to this point
http://multicore.zju.edu.cn/fatlab
2010.06. 浙江大学计算机学院 30
浙
江
大
学
软
Android Service Lifecycle
硬
件 The lifecycle for a service:
协
同 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
http://multicore.zju.edu.cn/fatlab
2010.06. 浙江大学计算机学院 31
浙
江
大
学
软
硬
件 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.
室
onDestroy
the onDestroy method is called when the service is about to be
http://multicore.zju.edu.cn/fatlab
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.
2010.06. 浙江大学计算机学院 32
浙
江
大
学
软
Permissions
硬
件 Android controls what applications are allowed to do by requiring
协 that they ask for permission to perform critical actions. When the
同 application is installed on a real device, the user can choose
whether to allow the requested permissions and proceed with
设 installation, or to reject installation (in the emulator environment,
计 it is assumed all permission requests are granted).
实 <uses-permission
验 android:name="android.permission.ACCESS_FINE_LOCATION" />
室 allows us to use fine-grained location providers, such as GPS.
<uses-permission
android:name="android.permission.ACCESS_LOCATION_EXTRA_COM
http://multicore.zju.edu.cn/fatlab
2010.06. 浙江大学计算机学院 33
浙
江
大
学
软
Connecting the phone
硬
件
协
>adb devices
同 List of devices attached
设 emulator-5554 device
计
实 HT840GZ12968 device
验
室
http://multicore.zju.edu.cn/fatlab
2010.06. 浙江大学计算机学院 34
浙
江
大
学
软
硬
件
协
同
设
计
实 Thank you!
验
室
http://multicore.zju.edu.cn/fatlab
2010.06. 浙江大学计算机学院 35