Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Introduction
Android Experience > Android Core Libraries (together with Noser) > Android Projects HikeTracker [swisstopo] SyncML Addressbook Synchronizer [Swisscom] Component Software Experience > Responsible for the Java Chapter of the Book on "Component Software"
AGENDA
Android Components > Activities Intents & Filters Task / Application Lifecycle Processes and Threads > Services > Content Providers
> C/C++
> Kernel
Android Components
Activity [User Interaction] > UI component typically corresponding to a screen E.g. Contacts: 3 activities: View contacts, Send message, Edit contact Service [Service Provider] > Background process without UI (e.g. mp3 player) Messages can be sent from and to a service [Data Provider] Content Provider > Enables applications to share data E.g. Contacts are provided to all applications [System Event Listener] Broadcast Receiver > Responds to external events, can wake up your process Phone rings, network activity established, time controlled
Activity
Activity is usually a single screen > Implemented as a single class extending Activity > Displays user interface controls (views) > Reacts on user input / events An application typically consists of several activities > Each screen is typically implemented by one activity > Moving to the next screen means starting a new activity > An activity may return a result to the previous activity
Intents
Intent > Intents are used to move from activity to activity > Intent describes what the application wants to do > Consists of Action to be performed (MAIN / VIEW / EDIT / PICK / DELETE / ) Categories Data to act on (URI) Extras (primitives / primitives[] / Strings / Serializable) Flags Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.fhnw.ch")); Intent intent = new Intent(Intent.ACTION_EDIT); intent.setData(Uri.parse("content://contacts/people/1"));
Intents
Intent Invocation > Launch new activity (without receiving a result)
startActivity(intent);
When activity exits, onActivityResult method is called with given requestCode (int > 0)
onActivityResult(int requestCode, int resultCode, Intent result)
Intents
Intent Specification > Intents define the interface between components > Intent repository on www.openintents.org
10
> Upon invocation of startActivity(intent) the system looks at the intent filters of all installed applications
11
12
13
APK Process
Activity Activity
Content Provider
Service
14
Process
Activity Activity
APK
Content Provider
APK
Content Provider
Service
Service
15
Home
Inbox
Details
Browse
Maps
> Comparable with EJB's stateful session beans (SFSB) > Each Android component has a managed lifecycle
16
Active / Running > Activity is in foreground > Activity has focus Paused > Still visible, partially overlaid > Focus lost Stopped > Activity is not visible Destroyed > Activity was terminated or killed
onSaveInstanceState() onPause()
onResume()
onStop()
Stopped Paused
<<kill>>
17
18
> If application is explicitly finished, this method is not called > Called before or after onPause onPause > Activity no longer TOS but still visible > New activity is not started until onPause returns onStop > Activity no longer visible onDestroy > Release resources; it is not guaranteed that this method is called
19
20
21
Process / Thread
Threading Overview > Each process has one thread (by default) => Single Threaded Model Threads and Loopers > Each thread has a Looper to handle a message queue > Events from all components are interleaved into the looper/Queue
APK
Process
Thread
Looper
1 1
Handler
Message Queue
22
Process / Thread
ActivityThread > Manages the main thread in an application process > Calls Looper.loop Looper.loop
while(true){ Message m=queue.next(); // may block if(m!=null){ m.target.dispatchMessage(m); m.recycle(); } }
Activity
APK Process
Thread Looper Activity Message Queue UI Events Local Service Calls
Intent Recvr
System Events
23
Process / Thread
Location Update in HikeTracker (onLocationChanged)
24
Process / Thread
Activity Result call-back (onActivityResult)
25
Process / Thread
Inactive Activities > If an activity does not consume events, the system assumes that the activity has a problem > AndroidNotResponding is shown No reaction to UI event within 5Sec No reaction to BroadcastEvent within 10Sec
26
checkRoot > Compares current thread with thread which created the view
27
28
> Other resources are only available by defined interfaces Services [exposes functionality] Content Provider [exposes data]
29
Android Processes
public class P1/P2 extends Activity { ... public void onStart(){ super.onStart(); Properties p = System.getProperties(); System.out.println("getProperties().id = " + System.identityHashCode(p)); System.out.println("getProperties().size() = " + p.size()); if(p.get("Android") == null){ p.put("Android", 0); } else { p.put("Android", ((Integer)p.get("Android"))+1); } System.out.println("P1/2: onStart " + p.get("Android")); System.out.println("getProperties().size() = " + p.size()); } }
30
Processes: Output
Output of Activity P1
INFO/System.out(12470): INFO/System.out(12470): INFO/System.out(12470): INFO/System.out(12470): getProperties().id = 1073834816 getProperties().size() = 37 P1: onStart 0 getProperties().size() = 38
Output of Activity P2
INFO/System.out(12504): INFO/System.out(12504): INFO/System.out(12504): INFO/System.out(12504): getProperties().id = 1073834816 getProperties().size() = 37 P2: onStart 0 getProperties().size() = 38
31
Android Processes
Zygote > Upon startup Zygote process is created which partially initializes a VM instance Core system libraries dex-files are loaded (mmap) Dalvik VM is initialized and shared heap (which contains Home objects used by all processes) is created APK > Upon start of an activity the Zygote process is forked Code Application specific dex files are loaded (mmap) Application specific heap (and VM structures) are initialized Maps => Core system libraries are shared! Zygote APK Code System Libraries
32
Service
Characteristics > Execution of long running tasks and business logic outside an activity E.g. a background task that has to download data periodically > Services can explicitly be started and stopped > Communication with service In-process if service runs in same APK Inter-Process Communication across APKs (AIDL)
33
Service
Explicit control > startService(intent) onCreate -> onStart > stopService(intent) onDestroy Implicit control > bindService(intent, conn, flags) onCreate -> onBind > unbindService(conn) onUnbind
34
35
IBinder
Binder
ICounterService
ICounterService.Stub CounterBinder
ICounterService.Stub.Proxy
36
37
Unbind Service
unbindService(donn);
38
Content Provider
Content Provider > The only way to share data between Android Packages > Implements a standard set of methods to provide access to data > Any form of storage can be used APK SQLite DB Process Files APK Activity Activity Remote Store
Process
Activity Content Provider
39
Content Provider
Content Provider Interface
abstract class ContentProvider { public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder); public Uri insert(Uri uri, ContentValues values) public int delete(Uri uri, String selection, String[] selectionArgs); String getType(Uri uri); public int update(ContentURI uri, ContentValues values, String selection, String[] selectionArgs) }
40
Content Provider
Access to Data Providers > Comparable to a DB Access (Cursor) > Identification via URI content://contacts/phones
String[] projections = new String[]{ "number", "name", "_id", "photo"}; Cursor cur = managedQuery( new ContentURI("content://contacts/phones"), projections, null, "name ASC");
41
42
43
dominik.gruntz@fhnw.ch