You are on page 1of 45

This watermark does not appear in the registered version - http://www.clicktoconvert.

com

Android Software Architecture


This watermark does not appear in the registered version - http://www.clicktoconvert.com

Android Software Architecture

2
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Android Application Design


This watermark does not appear in the registered version - http://www.clicktoconvert.com

Steps

§ Define application architecture

§ Design application UI layout

§ Identify the UI components

§ UI coding and associating actions with UI components

§ Application logic and associating it with UI events

§ Provide native interface for application

§ Write the JNI code

§ Link JNI with native code, and application logic with JNI

4
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Application Architecture Overview


§ An android application can be divided in following modules:

– Android application (apk)

– Functionality library (jar) – optional

– JNI layer (for interacting with native code)

§ Application functionality at Android level can be provided in 2 ways:


– Complete functionality as part of the application, which interacts
directly with the JNI layer (for example, DLNA application). [See figure
1]

– Functionality is provided by means of a library integrated in the


Android SDK, and application uses the APIs provided by this library (for
example, VT application). [See figure 2]

5
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Architecture without Functionality Library

Application apk

JNI Code

Shared library

Native Code

Figure-1: Application architecture without jar file, and placing of various components w.r.t. Android architecture

6
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Architecture with Functionality Library

Application apk

Functionality Library jar

JNI Code

Shared library

Native Code

Figure-2: Application architecture with jar file, and placing of various components w.r.t. Android architecture

7
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Application UI Layout

§ Here we need to focus on the UI flow of the application:


– Layout of various screens of the application. If both
landscape and portrait modes are to be supported, then
UI should be designed for both these modes.
– What happens when user interacts with a particular UI
component (for example, when user selects a menu item,
or clicks on a button)
§ All the possible scenarios/screenshots of the GUI should be
captured in this phase, and the flow should be clearly
defined.

8
This watermark does not appear in the registered version - http://www.clicktoconvert.com

UI Layout

r Implementations control the look and layout of screen


components

Title

High-level
Components

Ticker tape (Optional;


device manufacturer can
place it at the top or
bottom of the screen)
9
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Identify the UI components

§ Identify the UI components of each screen, like buttons,


menus, dialogs, input boxes, etc.

§ Identify which components can the user interact with and


which ones are non-interactive.

§ Identify which components are persistent (always visible,


e.g. background) and which ones are dynamic in nature, i.e.
need to be made visible/invisible according to some action
or state.

10
This watermark does not appear in the registered version - http://www.clicktoconvert.com

UI Coding

§ Create xml layout files for each screen (activity)


– Dynamic components should also be defined in the xml file
only with initial visibility defined
– Examples of simple and complex layout are attached

simple.xml complex.xml

§ Associate event handlers with interactive UI components, and


do the necessary UI updates/actions in these event handlers
– In android only the main UI thread can “touch” UI
components. So in case of multi-threaded applications, UI
update events should be passed to a message handler
running in main UI thread.

11
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Application Logic Coding

§ Write the application logic in Java files

§ Associate the UI events with the core application logic code

§ Any state machine to be implemented at UI level should be a


part of core application logic.

§ If both portrait and landscape mode are supported, handle the


orientation change
– This includes saving critical data (state machine, variables,
etc.) to a “Bundle”

12
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Native Interface

§ Declare the native interface, i.e. JNI functions to be invoked


from Java code
– Each functionality defined at native level should have a
corresponding JNI function

§ Define callback functions to be invoked from JNI code and


provide their functionality

§ Ideally there should be a separate file for JNI interaction so


that core application functionality is separated from native
interaction (though this might lead to extra function calls)

§ It is a good practice to define initialize() and deinitialize()


functions, which should be implemented in the native code

13
This watermark does not appear in the registered version - http://www.clicktoconvert.com

JNI Code

§ Implement the JNI functionality in C/C++ file.


§ The JNI file should be made a part of the library providing
native functionality.
§ Name of the library should be carefully decided so that it
does not clash with any system library.
§ Any data conversions (int-to-enum or vice-versa) should
be taken care of at JNI level.
– Enum values should not be passed using JNI, rather
their integral values.

14
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Putting It All Together


§ UI and Application Logic
– Ensure that the UI events are handled and invoke the proper application
logic functionality
– On application callback, UI should be updated accordingly

§ Application Logic and JNI


– Whenever a native functionality is to be invoked, appropriate JNI
function should be called
– JNI callbacks should be handled properly in the JNI interface, and
appropriate application logic function should be called

§ JNI and Native code


– JNI file should do the data conversions when sending/receiving data
to/from the Java application
– JNI file should invoke the native functionality, and provide a mechanism
for the native code to provide callbacks
– On receiving callback from native code, JNI file should invoke the
reverse JNI function exposed the the JNI interface of the application

15
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Code Flow
Application UI Application Code (Java) JNI Code (C/C++)

User action Invoke native


function

Callback from
JNI

Function call /
Update UI IPC

Core stack
functionality (C/C++)

16
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Android Application Designing


An Introduction
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Android Application Package


§ Android applications are written in Java.
§ An Android application is bundled by the aapt tool into an
Android package (.apk)

• res/layout: declaration layout files


• res/drawable: intended for drawing
• res/admin: bitmaps, animations for
transitions
• res/values: externalized values
Østrings, colors, styles, etc
• res/xml: general XML files used at runtime
• res/raw: binary files (e.g. sound)

18
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Structure of an Android Application

Android apps are


stored in
‘packages’
Resources:
Data, sound files, images

java
java aapt MyApp.apk
code data mp3
code

# library calls

•Android OS is multi-processing, multi-threading è multiple


processes can be running on the device at the same time.

• Processes can communicate w/ the OS, and also can


communicate w/ each other

19
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Parts of Application

§ Activity

§ Intent, IntentFilter, IntertReceiver

§ View

§ Service

§ Content provider
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Application Components

§Activity
– An activity is needed to create a screen for a user application.

§Intents
– Intents are used to transfer control from one activity to
another.

§ Services
– It doesn't need a user interface. It continues running in the
background with other processes run in the foreground.

§Content Provider
– This component allows the application to share information
with other applications.

21
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Major Building Blocks

§ Activity – user interface component, which corresponds to one screen at


time. It means that for the simple application like Address Book, the
developer should have one activity for displaying contacts, another
activity component for displaying more detailed information of chosen
name and etc.

§ Intent Receiver – wakes up a predefined action through the external


event. For example, for the application like Email Inbox, the developer
should have intent receiver and register his code through XML to wake
up an alarm notification, when the user receives email.

§ Service – a task, which is done in the background. It means that the


user can start an application from the activity window and keep the
service work, while browsing other applications. For instance, he can
browse Google Maps application while holding a call or listening music
while browsing other applications.

§ Content Provider – a component, which allows sharing some of the data


with other processes and applications. It is the best way to communicate
the applications between each other.

22
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Application Components
§ Android applications do not have a single entry point (e.g. no
main() function).
§ They have essential components that the system can
instantiate and run as needed.
§ Four basic components

Components Description
Activity UI component typically corresponding to one screen
Service Background process without UI
Broadcast Receiver Component that responds to broadcast Intents
Content Provider Component that enables applications to share data

23
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Components - Activity
§ An 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 screens:
– Each screen is implemented by one activity.
– Moving to the next screen means starting a new activity.
– An activity may return a result to the previous activity.

24
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Android components: Activities

•Activity is a sequence of related actions


•Activities are the most common Android component.
•Each activity presents a visual interface to the user
•Each activity is derived from base class Activity
•Each activity owns a View which controls a rectangular window;
•Child views (controlling sub-rectangles) can be derived from parents;
•Views are used to create images, icons, buttons, etc.
•They are typically a single screen of an application and must extend the
base “Activity” class.
•Additionally, activities can pass values back and forth and are saved in a
history stack.

Examples:
•The “Contacts” application may have an activity that displays a scrolling list
of all contacts listed by last name.

•The “Calculator” app may have an activity that displays a numeric keyboard
and buttons for numeric operations, etc. and awaits inputs from the user.

25
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Components - Activity (Cont)

§ Typically, one of the activities is marked as the first one that


should be presented to the user when the application is
launched.

§ Created “Activity” must be defined into the application’s


manifest.

26
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Android API – activity control loop

Colored ovals: states of the activity

Grey rectangles: callback methods


written by developer

source: developer.android.com

27
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Components - Service
§ A service does not have a visual user interface, but rather runs
in the background for an indefinite period time.
Example: music player, network download, etc
§ Each service extends the Service base class.
§ It is possible to bind to a running service and start the service
if it's not already running.
§ While connected, it is possible communicate with the service
through an interface defined in an AIDL (Android Interface
Definition Language).
Notification

Communication
Pause/rewind
/stop/restart

28
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Android components: Services


•Service is an activity that runs in the background è no visual
interface

•A service is code that runs for awhile without a UI.

•Each activity is derived from base class Service

Example:
•A common example of a service is an mp3 player that may run in
the background as the user may be involved with some activity of
another app, e.g. web browser.

NOTE:
•Typically, a service may be created, say, by an activity;
Alternatively, a service may be started and running in some other
context, and can announce its interface to other activities –
in this case, the activity may just connect itself to the service, in
Android, this is called “bind”-ing to the service.

29
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Components - Service (Cont)

§ Adding a "Service" with Android is quite similar than for an


"Activity".

30
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Android Service Life Cycle

§ void onCreate()

§ void onStart(Intent
intent)

§ void onDestroy()

31
31
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Components - Broadcast Receivers

§ A broadcast receiver is a component that receives and


reacts to broadcast announcements (Intents).
ü Many broadcasts originate in system code.
E.g. announcements that the time zone has changed,
that the battery is low, etc.

Broadcast Receiver Activity

•Get incoming calls


•Get incoming SMS

SMS

32
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Components - Broadcast Receivers (Cont)

§ A broadcast receiver is a component that receives and reacts


to broadcast announcements. (Cont)
ü Applications can also initiate broadcasts.
E.g. to let other applications know that some data has
been downloaded to the device and is available for
them to use.
§ All receivers extend the BroadcastReceiver base class.

33
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Android components: Broadcast receivers

•Broadcast receivers are similar to interrupt handlers in


normal OS BRs run in the background, listening for interrupts
generated by other apps.

•An application may have one or more BR’s to handle


interrupts.

Examples of interrupts:
-Incoming phone call

-User changed language setting

-Battery is low

•User has transited from one time zone to different one

34
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Components - Content Providers


Application

Activity
Activity Activity
Activity
Application Application
Content
Content
Activity
Activity Service
Service
Resolver
Resolver

Content
Content Content
Content
Content
Content Provider
Provider
Resolver
Resolver Resolver
Resolver

SQLite Remote
Data
Data XML
XML Store

§ A content provider makes a specific set of the application's


data available to other applications.
ü The data can be stored in the file system, in an SQLite,
or in any other manner that makes sense.

35
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Components - Content Providers (Cont)


§ Using a content provider is the only way to share data
between Android applications.
§ It extends the ContentProvider bas class and implements a
standard set of methods to allow access to a data store.
ü Querying
ü Delete, update, and insert data

§ Applications do not call these methods directly.


ü They use a ContentResolver object and call its methods
instead.
ü A ContentResolver can talk to any content provider.

§ Content is represented by URI and MIME type.

36
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Android components: Content providers

•Content providers

•make some subset of an application’s data available to


other apps when requested

•are the only mechanism for apps to share data.

•is a class that implements a standard set of methods to


let other applications store and retrieve the type of data
that is handled by that content provider.

37
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Android: Content Provider

§ Each provider can expose its data as a simple table on a


database model

§ Each content provider exposes a public URI that uniquely


identifies its data set:
– android.provider.Contacts.Phones.CONTENT_URI
android.provider.Contacts.Photos.CONTENT_URI
android.provider.CallLog.Calls.CONTENT_URI
android.provider.Calendar.CONTENT_URI

38
38
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Intent

§ An Intent object is passed to Context.startActivity() or


Activity.startActivityForResult() to launch an activity or get an existing
activity to do something new.

§ An Intent object is passed to Context.startService() to initiate a service


or deliver new instructions to an ongoing service.

§ Similarly, 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.

§ Intent objects passed to any of the broadcast methods (such as


Context.sendBroadcast(), Context.sendOrderedBroadcast(), or
Context.sendStickyBroadcast()) are delivered to all interested broadcast
receivers. Many kinds of broadcasts originate in system code.

39
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Intent

§ <Component name> [optional]


§ Action

§ Data, e.g., mpeg


§ Category, e.g., browser able

40
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Intent filter

action

category

data

41
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Intents

§ Intents are simple message objects each of which consists of


ü Action to be performed
(MAIN/VIEW/EDIT/PICK/DELETE/DIAL/etc)
ü Data to operate on (URI)

startActivity(new Intent(Intent.VIEW_ACTION,
Uri.parse("http://www.fhnw.ch"));

startActivity(new Intent(Intent.VIEW_ACTION,
Uri.parse("geo:47.480843,8.211293"));

startActivity(new
Intent(Intent.EDIT_ACTION,Uri.parse("content://contacts/people/1"));

42
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Intents (Cont)

§ Intent Filters
ü A component's intent filters in the manifest file inform
Android of the kinds of intents the component is able to
handle.
ü An example

43
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Intents (Cont)

§ Intent Filters (Cont)


ü An example (Cont)
A component can have any number of intent filters,
each one declaring a different set of capabilities.
The first filter in the example indicates that the
activity is the entry point for the application.
The second filter declares an action that the activity
can perform on a particular type of data.

44
This watermark does not appear in the registered version - http://www.clicktoconvert.com

Android Team
CEMT Products Group
BGH_Team_Android@aricent.com

Thanks For Listening!!!

45

You might also like