Intro To Android
Intro To Android
REMINDER
Attendance matters!
MOBILE OS
SOME REVISIONS ON PREVIOUS TOPIC
ANDROID APP
INTRODUCTION
ANDROID APP
INTRODUCTION
• Each installed android app is
contained inside a security
sandbox, which provide following
functionalities:
• Android OS is in multi-user
environment – each app is a single
user.
• System assigns app with unique
user ID for control access purposes.
• Each app runs in its own Linux
process.
• Each process runs independently
inside its own VM.
* Sandbox : security mechanism to run various running program separately.
[email protected]
8
ANDROID APP
WITHOUT SANDBOX
Unrestricted
All user access
data
Apps
All system
resources
ANDROID APP
WITH SANDBOX MECHANISM
Sandbox
Unrestricted
Other App user access
user data data
No access
Apps
Other App
system system
resources resources
ACTIVITY LIFECYCLE
ONCREATE()
▪ Fires when system first creates the
activity
▪ Perform basic startup logic that
should happened only once for the
entire life of the activity
▪ E.g., bind data to a list, instantiate
some class-scope variables
▪ After onCreate() method finishes its
execution, the activity enters Started
state, system calls
▪ OnStart()
▪ OnResume() in quick succession
16
ACTIVITY LIFECYCLE
ONSTART()
▪ Makes the activity visible to user
▪ App prepares for the activity to
enter the foreground and become
interactive
▪ E.g., initialize code that maintains
the UI
▪ Once the callback finishes, it enters
Resumed state
▪ System invokes onResume()
17
ACTIVITY LIFECYCLE
ONRESUME()
▪ State where the app interacts with
the user
▪ Stays in this state until something
happens to take focus away from
the app
▪ E.g., receiving phone call, user
navigates away to another app,
device’s screen turns off.
▪ When interruptive event occurs,
activity enters Paused state.
▪ System invokes onPause()
▪ If activity returns from Paused state,
system once again calls onResume()
▪ Should implement onResume() to
initialize components that release
during onPause()
18
ACTIVITY LIFECYCLE
ONPAUSE()
▪ System calls this method as the first
indication that user is leaving your
activity.
▪ Activity no longer in foreground
▪ Use onPause() to pause or adjust
operations that should not be
continued.
▪ onPause() execution is very brief
and does not necessarily afford
enough time to perform save
operation – may not complete
before method finishes.
▪ Remain until activity resume or
become completely invisible to user
▪ System calls onStop()
19
ACTIVITY LIFECYCLE
ONSTOP()
▪ No longer visible to user
▪ Entered Stopped state
▪ E.g., newly launched activity covers
entire screen / activity finishes,
system calls onStop()
▪ Release resources
▪ Perform CPU-intensive shutdown
operation
▪ Can also used to save information to
database
▪ Activity comes back interact with
user – calls onRestart(), OR
completely go away – calls
onDestroy()
20
ACTIVITY LIFECYCLE
ONDESTROY()
▪ Called before activity is destroyed
▪ Activity finishing
▪ System temporarily destroying the
activity due to configuration
changes (e.g., multi-window view)
▪ Clean up procedures before it is
destroyed
21
ACTIVITY LIFECYCLE
ONRESTART()
▪ Invoked when an activity currently
reside Stopped state is about the
restart.
▪ Restore the state of activity when it
was stopped.
▪ Usually followed by onStart()
22
ACTIVATING COMPONENT
VIA INTENT
• In Android system design, any app can start another
app’s component.
• E.g., taking photo using other app
• But because system runs each app in a separate
process with file permission that restrict access to other
apps, your app CANNOT directly activate a
component from another app.
• You need to use Android System. HOW?
• Deliver a message to the system that specifies your
INTENT to start a particular component, system will then
activate the component for you.
ACTIVATING COMPONENT
VIA INTENT
• An asynchronous message utilized to activate activity,
service and broadcast receiver.
• Bind individual component to each other during
runtime.
• Act as messenger that request an action from other
components, whether the component is belong to your
app or not.
• Can choose to activate either specific component
(explicit intent) or specific type of component (implicit
intent)
ACTIVATING COMPONENT
VIA INTENT
• For activity and service:
• Define action to perform (e.g., view, send, etc.)
• Specify the URI of data to act on
• E.g., issue intent to let user pick a personal contact and
return the URI of chosen contact.
• For broadcast receiver:
• Define the announcement being broadcast
ACTIVATING COMPONENT
VIA CONTENT RESOLVER
• Handle all direct transactions with content provider
• Provide a layer of abstraction between content
provider and component requesting information (for
security purposes).
METHODS TO ACTIVATE
COMPONENTS
• Start an activity
• Pass an Intent object to startActivity() or
startActivityForResult()
• Start a service
• Pass an Intent object to startService()
• Initiate broadcast
• Pass an Intent object to sendBroadcast() /
sendOrderedBroadcast() / sentStickyBroadcast()
• Query to content provider
• Call query() on ContentResolver object.
METHODS TO ACTIVATE
COMPONENTS
• Quick Question 2: Which of the following is NOT a
component in Android App?
A. Broadcast provider
B. Activity
C. Services
D. Content provider
• Other elements:
• <uses-feature> - hardware and software needed
• <uses-sdk> - API used
• <uses-library> - other libraries needed to be linked
• <uses-configuration> - specific input features required
• <uses-permission> - system permission needed to be
granted
• And others…
APP RESOURCES
• Android App needs more than code!
• Images, Audio, etc. that visualize the content
• XML files to define style, animation, colours and layout
• Alternative resources to define various languages and
device sizes
APP RESOURCES
• SDK build tools define unique integer ID for every
resources in the app
• E.g., if your app contains logo.png (saved in
res/drawable/ directory), SDK will generate a resource ID
named R.drawable.logo
• Can be used to reference the image and insert into the
UI of the app
• Define string translations to other languages that stored
in separate files, e.g., res / values-fr, for French
translation.
• Or use it to define the layout when different orientation
is in-used – the qualifier that define different device
configurations.
ASSIGNMENT
• Anyone else still looking for group?