Chapter Two: Building Blocks of Android

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

CHAPTER TWO

BUILDING BLOCKS OF ANDROID

LECTURE: ENG. MECAD ABDILAHI

Instaructor Eng. Mecad Abdilahi


EELO UNIVERSITY 1
Chapter 2. Main Building
Blocks
In this chapter, you will learn about the big blocks in
Android. We’ll give you a high-level overview of what
activities are, how intents work, why services are cool, how
to use broadcast receivers and content providers to make your
app scale, and much more.
By the end of this chapter, you will understand the main
Android components for building applications. You should
conceptually know when you’d use what component. You
will also see how these components relate to a real-world
application Instaructor Eng. Mecad Abdilahi
EELO UNIVERSITY 2
What Are Main Building Blocks?
The main building blocks are components that you
use as an application developer to build Android
apps. They are the conceptual items that you put
together to create a bigger whole. When you start
thinking about your application, it is good to take a
top-down approach. You design your application in
terms of screens, features, and the interactions
between them. You start with conceptual drawing,
something that you can represent in terms of “lines
and circles.” This approach to application
development helps you see the big picture—how the
components fit together and how it all makes sense.
Instaructor Eng. Mecad Abdilahi
EELO UNIVERSITY 3
Building Blocks

4
Building Blocks
3
 Activity
 Intent
 Services

 style
Broadcast Receiver
 Content Provider

Instaructor Eng. Mecad Abdilahi


EELO UNIVERSITY 5
Activity
The Activity is the basic building block
4
of every visible Android application.
Every screen in an application is an
activity by itself.
They work together to present an
application sequence, each activity is an
independent entity. Activities typically
involve an interaction with a user.

Instaructor Eng. Mecad Abdilahi


EELO UNIVERSITY 6
Intent
Intent is used to invoke components. 5
It is mainly
used to:
 Start the service
 Launch an activity
 Display a web page
 Display a list of contacts
 Broadcast a message
 Dial a phone call etc.

Instaructor Eng. Mecad Abdilahi


EELO UNIVERSITY 7
Intents
Intents are messages that are sent
among the major building blocks. They
trigger an activity to start up, tell a
service to start or stop, or are simply
broadcasts. Intents are asynchronous,
meaning the code that sends them
doesn’t have to wait for them to be
completed.

Instaructor Eng. Mecad Abdilahi


EELO UNIVERSITY 8
Example intent

Instaructor Eng. Mecad Abdilahi


EELO UNIVERSITY 9
Services
6
Service is a background process that can
run fora long time.
There are two types of services local and remote.
Local service is accessed from within the
application
whereas remote service is accessed remotely from
other applications running on the same device.

Instaructor Eng. Mecad Abdilahi


EELO UNIVERSITY 10
services
Services have a much simpler life cycle
than activities (see Figure 4-3). You either
start a service or stop it. Also, the service
life cycle is more or less controlled by the
developer, and not so much by the system.
Consequently, we as developers have to
be mindful to run our services so that they
don’t consume shared resources
unnecessarily, such as the CPU and
battery.
Instaructor Eng. Mecad Abdilahi
EELO UNIVERSITY 11
services

Instaructor Eng. Mecad Abdilahi


EELO UNIVERSITY 12
Broadcast Receivers
The system itself broadcasts events all the
time. For example, when an SMS arrives, a
call comes in, the battery runs low, or the
system gets booted, all those events are
broadcasted, and any number of receivers
could be triggered by them.
You can also send your own broadcasts from
one part of your application to another, or to
a totally different application.
Content Provider
The Content Providers are components that 8
expose a specific set of data to applications.
They provide the tools to manage access and
define security for the data.
If data is only going to be used in your application,
then a content provider is unnecessary. It is only
needed to communicate and share that data outside
the application.

Instaructor Eng. Mecad Abdilahi


EELO UNIVERSITY 14
Content Provider

Instaructor Eng. Mecad Abdilahi


EELO UNIVERSITY 15
Content providers
Content providers are relatively simple
interfaces, with the standard insert(),
update(), delete(), and query() methods.
These methods look a lot like standard
database methods, so it is relatively easy
to implement a content provider as a proxy
to the database. Having said that, you are
much more likely to use content providers
than write your own.
Instaructor Eng. Mecad Abdilahi
EELO UNIVERSITY 16
The Activity lifecycle
• storage: Your device has apps and files 3
installed and stored on its internal disk, SD
card, etc.
 Settings Storage
memory: Some subset of apps might be
currently loaded into the device's RAM and are
either running or ready to be run.
 When the user loads an app, it is loaded from
storage into memory.
 When the user exits an app, it might be cleared
from memory, or might remain in memory so
you can go back to it later. Instaructor Eng. Mecad Abdilahi
EELO UNIVERSITY 17
Continue
Activity Manager is responsible for creating,
destroying, and managing activities. For
example, when the user starts an application
for the first time, the Activity Manager will
create its activity and put it onto the screen.
Later, when the user switches screens, the
Activity Manager will move that previous
activity to a holding place. This way, if the
user wants to go back to an older activity, it
can be started more quickly.
Instaructor Eng. Mecad Abdilahi
EELO UNIVERSITY 18
Activity lifestyle
7

Instaructor Eng. Mecad Abdilahi


EELO UNIVERSITY 19
Starting state
When an activity doesn’t exist in memory,
it is in a starting state. While it’s starting
up, the activity will go through a whole
set of callback methods that you as a
developer have an opportunity to fill out.
Eventually, the activity will be in
a running state.

Instaructor Eng. Mecad Abdilahi


EELO UNIVERSITY 20
Running state
The activity in a running state is the one that is currently
on the screen and interacting with the user. We also say
this activity is in focus, meaning that all user interactions
—such as typing, touching the screen, and clicking
buttons—are handled by this one activity. As such, there
is only one running activity at any given time.
The running activity is the one that has priority in terms
of getting the memory and resources it needs to run as
quickly as possible. This is because Android wants to
make sure the running activity is zippy and responsive to
the user.

Instaructor Eng. Mecad Abdilahi


EELO UNIVERSITY 21
Paused state
When an activity is not in focus (i.e., not
interacting with the user) but still visible
on the screen, we say it’s in a paused
state..

Instaructor Eng. Mecad Abdilahi


EELO UNIVERSITY 22
Stopped state
When an activity is not visible, but still in
memory, we say it’s in a stopped state.
Stopped activity could be brought back to
the front to become a Running activity
again. Or, it could be destroyed and
removed from memory.
The system keeps activities around in a
stopped state because it is likely that the
user will still want to get back to those
activities some time soon,
Instaructor Eng. Mecad Abdilahi
EELO UNIVERSITY 23
Destroyed state

ItBefore the activity is destroyed, it can


perform certain actions, such as save any
unsaved information. However, there’s no
guarantee that your activity will be stopped
prior to being destroyed. It is possible for a
paused activity to be destroyed as well. For
that reason, it is better to do important work,
such as saving unsaved data, in route to a
paused state rather than a destroyed state.

Instaructor Eng. Mecad Abdilahi


EELO UNIVERSITY 24
Additional Components

 Views : UI elements that are drawn on-screen 9


including buttons, list forms etc.

 Layouts : View hierarchies that control screen


format and appearance of the views.

 Manifest : Configuration file for the application


Resources
Android supports that resources like images and certain XML
configuration files, can be kept separate from the source code.

Instaructor Eng. Mecad Abdilahi


EELO UNIVERSITY 25
Notifications
10
Android allows to put notification into the title
bar of your application. The user can expand
the notification bar and by selecting the
notification the user can trigger another
activity.
Types of Notifications:
1. Toast Notifications
2. Status Bar Notifications
3. Dialog Notifications

Instaructor Eng. Mecad Abdilahi


EELO UNIVERSITY 26
Summary
In this chapter, you learned about some of the
most important Android application
components. We put together these
components to create various applications,
from a simple Hello World to much more
complex creations.
In the next chapter, we’ll outline a Yamba
application as an example of how all these bits
and pieces come together to form a working
Android app.

Instaructor Eng. Mecad Abdilahi


EELO UNIVERSITY 27
http://developer.android.com

You might also like