Android Fundamentals
- Abhin
October 4, 2019 © 2008 Wipro Ltd - Confidential 1
Topics
Introduction
Components of Android
Activities and Intents
Services and Notification
Broadcast receivers
Content Providers
Processes and lifecycles
Introduction
• Android applications written in Java.
• Java code + data/resource -> bundled by aapt
-> .apk file
• Each android app runs in its own world
- runs its own linux process
- has its own java VM
- is assigned a unique linux user ID
Application Components
• The fundamental building blocks / components of
Android are-
1- Activities
2- Services
3- Broadcast Receivers
4- Content Providers
• Activity
- Basic building block of an android app
- Means to render UI
Application Components
• Service
- another building block
- No UI
- Runs in the background for an indefinite period
• Broadcast Receiver
- receives and responds to broadcast messages
• Content Providers
- expose specific set of data to applications
Activity and Intents
• The means of communication between these
components are
1- Intents
2 – Intent filters
• Intents
- messages passed between different components
• A component expresses its ‘intent’ to do a job
• A component which has advertised that it can do this
job through ‘intent filters’ is invoked.
Activity and Intents
• Intent - a passive data structure that holds an abstract
description of the operation to be performed .
• Intent Filter - means through which a component advertizes its
own capabilities to handle specific job.
• Advantages – mix and match of components , code reusability…
• The UI elements are
1- Views
2- Notifications
Activity and Intents
• Two types of intents – Explicit and Implicit
• Explicit intents -specify the activity that is required to
respond to the intent
• Implicit intents - does not name a target component
that should act upon the intent
• Android platform resolves as to which component is
best suited to respond to an Implicit Intent
Activity and Intents
• Calling activity can receive data from the called
activity
• methods to implement in the parent activity:
1. startActivtyForResult()
2. onActivityResult()
• The child Activity should complete the work as usual
and finally call:
1. setResult()
2. finish()
Activity lifecycle
• Application components have a lifecycle
• An activity has three states
-active or running , paused , stopped
• An activity transitions from state to state by calls to
the following protected methods –
void onCreate(Bundle savedInstanceState)
void onStart()
void onRestart()
void onResume()
void onPause()
void onStop()
void onDestroy()
Activity lifecycle
Services and Notification
• Notification - way of alerting a user about an event that he
needs to be informed about or even take some action on getting
that information
• Types - Status Bar Notification, Vibrate, Flash lights, Play sound
• Service doesn't have a visual user interface
• Runs in the background for an indefinite period of time
• Possible to bind to a service with an interface.
• Services run in the main thread of application
Service lifecycle
• A service has fewer lifecycle methods –
void onCreate()
void onStart(Intent intent)
void onDestroy()
• onStart() is called only for services started by
startService()
Service lifecycle
Broadcast receivers
• Component to receive and respond to broadcast
messages
• Most broadcasts originate in system code
• Applications can initiate broadcasts
• No UI to display
• May initiate an activity or notification.
Broadcast receiver lifecycle
• A broadcast receiver has single callback method
void onReceive(Context curContext, Intent broadcastMsg)
• Broadcast receiver is considered to be active only while
it is executing this method
• A process with an active broadcast receiver is
protected from being killed
Content Providers
• Content provider makes a specific set of the
application's data available to other applications
• Deals with stored data – files, databases, preferences
• A ContentResolver interfaces with a content provider
ContentResolver cr = getContentResolver();
• Content providers expose their data as a simple table
on a database model
• Each content provider exposes a public URI that
uniquely identifies its data set
Content Providers
• A content provider may control multiple data sets
android.provider.Contacts.Phones.CONTENT_URI
android.provider.Contacts.Photos.CONTENT_URI
• A URI is declared as
Uri mContacts = Phones.CONTENT_URI;
• A query returns a Cursor object that can move from record to record
• To create a content provider –
1-Set up a system for storing the data
2-Extend the ContentProvider class to provide access to the data
3-Declare the content provider in the manifest file for your application
• Six abstract methods declared in the ContentProvider class:
query(), insert(), update(), delete(), getType(), onCreate()
Activating components
• All components other than content providers are
activated by intents
• Activity is launched by Context.startActivity() or
Activity.startActivityForResult()
• Result is returned in an Intent object onActivityResult()
• Service is started by Context.startService()
• Context.bindService() establishes connection between
the calling component and a target service
• A broadcast can be initiated by methods like
Context.sendBroadcast()
Shutting down components
• No need to explicitly shut down content provider and
broadcast receiver
• Activity can be shut down by calling its finish()
• Service can be stopped by calling its stopSelf() method,
or by calling Context.stopService()
The manifest file
• Applications declare their components in a manifest
file
• Manifest is a structured XML file
• Names libraries the application needs to be linked to
• Identifies any permissions the application expects to be
granted
• Broadcast receivers can either be declared in the
manifest or can be created in code
Activities and Tasks
• A task is what the user experiences as an "application"
• A group of related activities, arranged in a stack
• Root activity is the one that began the task
• Activity at the top of the stack is one that's currently
running
• All the activities in a task move together as a unit
• The affinities of activities to tasks can be controlled
• Intent flags - FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_CLEAR_TOP,
FLAG_ACTIVITY_RESET_TASK_IF_NEEDED …
• <activity> attributes – taskAffinity, allowTaskReparenting ….
Saving activity state
• The system may shut down an activity
• The user expects to return to the activity and find it in
its previous state
• An onSaveInstanceState() method has to be
implemented for the activity
• This is called before onPause() is called
• The Bundle is passed both to onCreate() and onStart()
Processes and lifecycles
• To determine which processes to keep and which to
kill, Android places each process into an "importance
hierarchy“.
• There are five levels in the hierarchy –
1- Foreground process
2- Visible process
3- Service process
4- Background process
5- Empty process
Implicit intents
Thank You
For further reference:
http://d.android.com/guide/topics/fundamentals.html
October 4, 2019 © 2008 Wipro Ltd - Confidential 26