Android Development: Notes Are Based On
Android Development: Notes Are Based On
Android Development: Notes Are Based On
Chapter 1 - Goals
THE BIG PICTURE
Chapter 1 - Resources
Androids web page http://www.android.com/
What is Android?
Android is an open-source software platform created by Google and the Open Handset Alliance.
What is Android?
Android consists of a complete set of software components for mobile devices including:
an operating system, middleware, and embedded key mobile applications a large market.
Why Android?
Listen from the project creators/developers
Nick Sears. Co-founder of Android Steve Horowitz. Engineering Director Dam Morrill. Developer Peisun Wu. Engineering Project Manager Erick Tseng. Project Manager Iliyan Malchev. Engineer Mike Cleron. Software Manager Per Gustafsson. Graphics Designer. etc
(2.19 min)
http://www.youtube.com/watch?v=6rYozIZOgDk&eurl=http://www.android.com/about/&feature=player_embedd ed
You will hear statements such as currently it is too difficult to make new products open software brings more innovation choices lower costs more applications such as family planner, my taxes, understand my wife better,
6
Commercializat.
Aplix Noser Engineering Astonishing Tribe Wind River Systems Omron Software Teleca
Semiconductor
Audience Broadcom Corp. Intel Corp. Marvell Tech. Group Nvidia Corp. Qualcomm SiRF Tech. Holdings Synaptics Texas Instr. AKM Semicond. ARM Atheros Comm ... EMP
Handset Manf
ACER ASUS HTC LG Motorola Samsung ASUSTek Garmin Huawei Tech LG Samsung Sony Ericsson Toshiba
Short video (4 min.) Showing Dave Bort and Dan Borstein, two members of the Android Open Source Project talk about the project.
Android is a software environment built for mobile devices. It is not a hardware platform. Android includes:
Linux kernel-based OS, a rich UI, telephone functionality, end-user applications, code libraries, application frameworks, multimedia support, ...
Operators
Device Manufacturers
Software Vendors
11
Not so long ago 1. Phone 2. Pager 3. PDA Organizer 4. Laptop 5. Portable music player 6. No Internet access / limited access
Tomorrow ?
12
14
15
16
Android Components
Application framework enabling reuse and replacement of components Dalvik virtual machine optimized for mobile devices Integrated browser based on the open source WebKit engine Optimized graphics powered by a custom 2D graphics library; 3D graphics based on the OpenGL ES specification (hardware acceleration optional) SQLite for structured data storage Media support for common audio, video, and still image formats (MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, GIF) GSM Telephony (hardware dependent) Bluetooth, EDGE, 3G, 4G, and Wi-Fi (hardware dependent) Camera, GPS, compass, and accelerometer (hardware dependent) Rich development environment including a device emulator, tools for debugging, memory and performance profiling, and a plugin for the Eclipse IDE
17
Android Components
18
Android Components
Video 1/3: Androids Architecture Presented by Mike Cleron, Google Corp. (13 min)
19
Android Components
Video 2/3: Applications Life Cycle Presented by Mike Cleron, Google Corp. (8 min)
20
Android Components
Video 3/3: Androids API Presented by Mike Cleron, Google Corp. (7 min)
21
http://sites.google.com/site/io/inside-the-android-application-framework
Android is designed to be fast, powerful, and easy to develop for. This session will discuss the Android application framework in depth, showing you the machinery behind the application framework.
explains the life-cycle of an android apk. very good!
22
Android Components
Video: An Introduction to Android
(about 52 min)
Presented by Jason Chen Google At Google Developer Conference San Francisco - 2008 Available at:
http://www.youtube.com/watch?v=x1ZZ-R3p_w8
23
24
26
Tm quan trng ca intent : chng cho php di chuyn iu khin trong ng dng v l kha cnh quan trng nht trong lp trnh Android.
27
Mt IntentFilter l mt trigger, mt khai bo v nng lc cung cp h tr cho nhng ni cn. Mt IntentFilter c th khai bo tng qut hoc c th v cc intent m n c th phc v.
28
Action Action cn thc hin, chng hn: ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, v.v..
29
30
Dissecting Intents
1. Component name Tn component cn x l intent ( v d "com.example.project.app.MyActivity1" ). Action Mt string l tn ca action cn thc hin hoc, i vi cc broadcast intent, action xy ra v ang c bo co kt qu (v d: ACTION_VIEW, ACTION_CALL, ACTION_TIMEZONE_CHANGED, ).
2.
3.
Data URI ca d liu cn x l v kiu MIME ca d liu (v d tel:/216 555-1234 , "http://maps.google.com, ... ).
Category mt string cha cc thng tin b sung v loi component cn x l intent (v d CATEGORY_BROWSABLE, CATEGORY_LAUNCHER, ). Extras cc cp key-value cha cc thng tin b sung cn gi cho component s x l intent. Flags cc c thuc nhiu loi.
31
4.
5.
6.
Delivering Intents
Mt i tng Intent c truyn cho Context.startActivity() hoc Activity.startActivityForResult() gi mt activity (ng b) hoc yu cu mt activity sn c lm g (khng ng b). Mt i tng Intent c truyn cho Context.startService() bt mt service hoc gi lnh mi cho mt service ang chy. C th truyn intent cho Context.bindService() thit lp kt ni gia component ang gi v component b gi. N c th bt service nu service cha phi ang chy.
32
Intent Resolution
C hai loi Intent: Explicit intent ch nh component ch bng tn, thng c dng khi mt activity bt mt service con hoc chy mt activity khc. Implicit intent khng gi tn component ch (trng dnh cho tn component trng), thng c dng kch hot cc component trong cc ng dng khc. Late binding c p dng y. Mi khi c th, Android s gi explicit intent cho mt thc th ca lp ch c ch nh.
33
35
37
38
IntentFilters
IntentFilter nh ngha quan h gia Intent v ng dng. IntentFilter c th quy nh c th n phn data, hoc phn action ca intent, hoc c hai. IntentFilter cn cha mt trng c gi l mt category. Category gip phn loi cc action. V d, category c tn CATEGORY_LAUNCHER ch dn Android rng Activity cha IntentFilter ny cn c nhn thy ti home screen.
39
IntentFilters
Khi mt Intent c pht i, h thng s nh gi cc Activity, Service hin c, v cc BroadcastReceiver c ng k, ri gi Intent ti ni nhn ph hp nht (Xem hnh sau).
40
IntentFilters
41
IntentFilters
bo cho h thng bit mnh c th x l cc implicit intent no, cc activity, service, v broadcast receiver c th c mt hoc nhiu intent filter. Mi filter m t mt nng lc m component mun nhn. Mt explicit intent lun c gi n ch ca n, bt k n cha g trong; khng cn hi n cc filter. Nhng mt implicit intent ch c gi n mt component khi v ch khi n lt qua mt trong cc filter ca component.
42
IntentFilters
IntentFilters are often defined in an applications AndroidManifest.xml with the <intent-filter> tag.
<intent-filter . . . > <action android:name="code android.intent.action.MAIN" /> <category android:name="code android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:type="video/mpeg" android:scheme="http" . . . /> <data android:type="audio/mpeg" android:scheme="http" . . . /> ... </intent-filter>
43
Android Applications
Mi ng dng Android chy trong tin trnh Linux ca ring n. Mi ng dng bao gm mt t hp cc component phn mm, trong c:
Activity Service BroadcastReceiver ContentProvider
44
Android Applications
Cu trc mt ng dng Android in hnh
45
Android Services
Service l mt component chy trong background, khng tng tc vi ngi dng, chy trong khong thi gian khng xc nh. Mi lp service phi c mt khai bo <service> tng ng trong AndroidManifest.xml. Service c th c bt/tt bng
Context.startService() v Context.bindService(). stopService() and unbindService()
46
Android Services
Service, cng nh cc i tng ng dng khc, chy trong thread chnh ca tin trnh ch. Ngha l, nu mt service nh lm thao tc g chim nhiu thi gian CPU (chng hn MP3 playback) hoc blocking (chng hn networking, RSS exchange), n cn sinh thread ring lm cng vic
47
Android Services
Service1 Class package matos.service; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class Service1 extends Service implements Runnable { private int counter = 0; @Override public void onCreate() { super.onCreate(); Thread aThread = new Thread(this); aThread.start(); } public void run() { while (true) { try { Log.i("service1", "service1 firing : # " + counter++); Thread.sleep(10000); //this is where the heavy-duty computing occurs } catch (Exception ee) { Log.e("service1", ee.getMessage()); } } } @Override public IBinder onBind(Intent intent) { return null; } }
48
Android Services
// Service1Driver package matos.service; import android.app.Activity; import android.content.Intent; import android.os.Bundle; public class Service1Driver extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // invoking the service Intent service1Intent = new Intent( this, Service1.class ); startService( service1Intent ); // do some work here. } }// Service1Driver
49
Android Services
Service1Demo Manifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="matos.service" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Service1Driver" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="Service1" android:enabled="true" > </service> </application> <uses-sdk android:minSdkVersion="3" /> </manifest>
50
Android Services
Debugging - Log Cat
07-01 02:49:46.097: INFO/ActivityManager(583): Displayed activity matos.service /.Service1 Driver 07-01 02:49:51.277: DEBUG/dalvikvm(724): GC freed 1575 objects / 81280 bytes in 138ms
07-01 02:49:55.831: INFO/service1(767): service1 firing : # 1 07-01 02:50:05.839: INFO/service1(767): service1 firing : # 2 07-01 02:50:15.847: INFO/service1(767): service1 firing : # 3 07-01 02:50:25.857: INFO/service1(767): service1 firing : # 4
51
Nu BroadcastReceiver cn thc thi m nhiu hn mc tm thng, phn m nn yu cu mt Service thc hin cng vic (bng Context.startService())
53
sendBroadcast()
BroadcastReceiver
onReceive()
BroadcastReceiver onReceive()
54
55
57
import android.util.Log; import android.app.Activity; import android.os.Bundle; public class MySMSMailBox extends Activity { // intercepts reception of new text-messages
58
registerReceiver(mySmsReceiver, filter);
}
59
61
62
63
65
66
The ContentResolver method takes an URI as its first argument. It's what identifies which provider the ContentResolver should talk to and which table of the provider is being targeted.
67
If you're querying a particular record, you also need the ID for that record. A query returns a Cursor object that can move from record to record and column to column to read the contents of each field. It has specialized methods for reading each type of data.
68
public class AndDemo1 extends Activity { /** queries contact list */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
// Use the ContentUris method to produce the base URI for the contact with _ID == 23. Uri myPerson1 = ContentUris.withAppendedId(People.CONTENT_URI, 23); // use the "people" content provider to explore all your contacts
69
70
71
<action> <activity> <activity-alias> <application> <category> <data> <grant-uri-permission> <instrumentation> <intent-filter> <manifest> <meta-data>
<permission> <permission-group> <permission-tree> <provider> <receiver> <service> <uses-configuration> <uses-library> <uses-permission> <uses-sdk>
72
73
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="matos.earthquake" android:versionCode="1" android:versionName="1.0.0"> <application android:icon="@drawable/yellow_circle" android:label="@string/app_name">
<activity android:name=".AndQuake" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SatelliteMapping"> </activity>
<service android:name="AndQuakeService" android:enabled="true" > </service> <receiver android:name="AndQuakeAlarmReceiver" > <intent-filter> <action android:name = "ALARM_TO_REFRESH_QUAKE_LIST"/> </intent-filter> </receiver> </application> <uses-library android:name="com.google.android.maps" /> <uses-permission android:name="android.permission.INTERNET" /> </manifest>
74
75
76
public class Currency1 extends Activity { // naive currency converter from USD to Euros & Colones
final double EURO2USD = 1.399; final double COLON2USD = 0.001736; // GUI widgets Button btnConvert; Button btnClear; EditText txtUSDollars; EditText txtEuros; EditText txtColones;
77
80
<TextView android:id="@+id/caption1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Currency Converter v0.01" android:textSize="18sp" android:textStyle="bold" > </TextView> <TextView android:id="@+id/greenFiller1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ff006666" > </TextView>
81
<Button android:id="@+id/btnConvert" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" Convert " android:layout_x="10px" android:layout_y="190px" > </Button>
<Button android:id="@+id/btnClear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" Clear " android:layout_x="90px" android:layout_y="190px" > </Button> </AbsoluteLayout> </LinearLayout>
82
83
Additional Resources
Google Developer Conference San Francisco 2009 Web page: http://code.google.com/events/io/
84
Questions ????
85