Android Chapter 01 Intro
Android Chapter 01 Intro
Android Chapter 01 Intro
Victor Matos
Cleveland State University
Notes are based on:
Unlocking Android by Frank Ableson, Charlie Collins, and Robi Sen. ISBN 978-1-933988-67-2 Manning Publications, 2009. & Android Developers http://developer.android.com/index.html
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. It is primarily used to power mobile phones.
It has the capability to make inroads in many other (non-phone) embedded application markets.
4
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, and WiFi (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
25
an open platform upon which Google, and potentially the open source community, can improve as necessary.
26
27
Intents are important because they facilitate navigation and represent the most important aspect of Android coding.
28
An IntentFilter is a trigger, a declaration of capability and interest in offering assistance to those in need. An IntentFilter may be generic or specific with respect to which Intents it offers to service.
29
30
Dissecting Intents
1. Component name The name of the component that should handle the intent ( for example "com.example.project.app.MyActivity1" ). Action A string naming the action to be performed or, in the case of broadcast intents, the action that took place and is being reported (for example: ACTION_VIEW, ACTION_CALL, ACTION_TIMEZONE_CHANGED, ). Data The URI of the data to be acted on and the MIME type of that data (for example tel:/216 555-1234 , "http://maps.google.com, ... ). Category A string containing additional information about the kind of component that should handle the intent (for example CATEGORY_BROWSABLE, CATEGORY_LAUNCHER, ). Extras Key-value pairs for additional information that should be delivered to the component handling the intent. Flags of various sorts.
2.
3.
4.
5.
6.
32
Delivering Intents
An Intent object is passed to Context.startActivity() or Activity.startActivityForResult() to launch an activity or get an existing activity to do something new (asynchronous & synchronously respectively). An Intent object is passed to Context.startService() to initiate a service or deliver new instructions to an ongoing service. An intent can be passed to Context.bindService() to establish a connection between the calling component and a target service. It can optionally initiate the service if it's not already running.
33
Intent Resolution
Intents can be divided into two groups: Explicit intents designate the target component by its name, typically used for an activity starting a subordinate service or launching a sister activity. Implicit intents do not name a target (the field for the component name is blank). Implicit intents are often used to activate components in other applications. Late binding applies. Whenever possible Android delivers an explicit intent to an instance of the designated target class.
34
36
38
39
IntentFilters
The IntentFilter defines the relationship between the Intent and the application. IntentFilters can be specific to the data portion of the Intent, the action portion, or both. IntentFilters also contain a field known as a category. A category helps classify the action. For example, the category named CATEGORY_LAUNCHER instructs Android that the Activity containing this IntentFilter should be visible in the home screen.
40
IntentFilters
When an Intent is dispatched, the system evaluates the available Activities, Services, and registered BroadcastReceivers and routes the Intent to the most appropriate recipient (see next Figure).
41
IntentFilters
42
IntentFilters
To inform the system which implicit intents they can handle, activities, services, and broadcast receivers can have one or more intent filters. Each filter describes a capability that the component is willing to receive. An explicit intent is always delivered to its target, no matter what it contains; the filter is not consulted. But an implicit intent is delivered to a component only if it can pass through one of the component's filters.
43
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>
44
Android Applications
Each Android application runs in its own Linux process. An application consists of a combination of software components including:
Activities Services Broadcast Receivers Content Providers
45
Android Applications
Structure of a typical Android Application
46
Android Services
A Service is an application component that runs in the background, not interacting with the user, for an indefinite period of time. Each service class must have a corresponding <service> declaration in its package's AndroidManifest.xml. Services can be started/stopped with
Context.startService() and Context.bindService(). stopService() and unbindService()
47
Android Services
Services, like other application objects, run in the main thread of their hosting process. This means that, if your service is going to do any CPU intensive (such as MP3 playback) or blocking (such as networking, RSS exchange) operations, it should spawn its own thread in which to do that work
48
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; } }
49
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 ); } }// Service1Driver
50
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>
51
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
52
54
BroadcastReceiver onReceive()
BroadcastReceiver onReceive()
55
Broadcasting an Intent is a background operation that the user is not normally aware of.
56
Ordered broadcasts (sent with sendOrderedBroadcast) are delivered to one receiver at a time. As each receiver executes in turn, it can propagate a result to the next receiver, or it can completely abort the broadcast so that it won't be passed to other receivers. The order receivers run in can be controlled with the android:priority attribute of the matching intent-filter; receivers with the same priority will be run in an arbitrary order.
57
58
import android.util.Log; import android.app.Activity; import android.os.Bundle; public class MySMSMailBox extends Activity { // intercepts reception of new text-messages
59
registerReceiver(mySmsReceiver, filter);
}
60
62
63
64
66
67
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.
68
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.
69
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
70
71
72
<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>
73
<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>
75
76
77
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;
78
81
82
83
84
Additional Resources
Google Developer Conference San Francisco 2009 Web page: http://code.google.com/events/io/
85
480 m
Papers
4 bn
Mobile Phone worldwide (half the population of the planet)
1.5bn
Televisions worlwide
$130 bn Messaging
$600 bn Voice
$70 bn Non-messaging
87
1.94% Yahoo
97.57% Google
2.43%
88
49.5% iPhone
89
9% Windows
6% Other 2% Android
51% Symbian
90
91
92
93
Questions ????
94