Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Chapter 2-Android Application Fundamentals

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

Chapter two

Android Application
Fundamentals

1
Contents
In this chapter, you will learn
 Activities
 Intents
 Services
 Providers
 Receivers.
 Application context

2
Main Building Blocks
 In this chapter, you will learn Android’s capabilities

what activities are, how intents work, when and how
to use services,

how to use broadcast receivers and content providers
to make your app scale, and much more.
 By “main building blocks,” we refer to the pieces
of an application that Android offers you to put
together into an Android app. When you start
thinking about your application,
 it is good to take a top-down approach. For
instance, most programmers design applications in
terms of screens, features, and the interactions
between them.
 You start with a 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.
 You should conceptually know when you’d use 3
A Real-World Example
 app to use Twitter.

We know that the user should be able to post
status updates.

We also know the user should be able to see
what her up to(timeline).

the user should also be able to update her
username and password for the online account.
 So now we know we should have the following
three screens:

a screen for users to update their own status,

a screen to see the status timelines of their
friends, and

a screen to set the preferences for the app.

4
A Real-World Example cont..
 To achieve this the app needs
 The network connection : To achieve that, the app
has to pull the data from the cloud when it’s online
and cache the data locally.

That will require a service that runs in the
background as well as a database(content
provider).
 We also know that we’d like this background
service to be started when the device is initially
turned on, so by the time the user first uses the
app, there’s already up-to-date information in the
local cache.

this will require broadcasting receiver
 Finally, we will want to display the latest status
from the friends’ timelines on the home screen, as
an Android Widget.(this will require Activity)

5
Contd.

6
Activities
 An activity is usually a single screen that the user
sees on the device at one time.
 An application typically has multiple activities, and
the user flips back and forth among them.
 Activities are the most visible part of your
application.

7
example

8
Activity Life Cycle
 The activity life cycle is managed by the Activity
Manager, a service that runs inside the Android
Framework layer of the stack.
 The 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.
States That an activity can go through

Starting

Running

Stopped

Paused

Destroyed
9
Activity Life Cycle states

10
Starting state
 When an activity doesn’t exist in memory, it is in a
starting state.
 As it starts, the activity invokes a set of callback
methods that you as a developer have an
opportunity to fill out. These callbacks include
onCreate(), onStart(), and onResume().
 Eventually, the activity will be in a running state,
which means that it will be fully displayed on the
screen, in focus, waiting for user to interact with it.
 Keep in mind that this transition from starting
state to running state is one of the most expensive
operations the application will perform in terms of
computing time, and this also directly affects the
battery life of the device.

11
Running state
 Only one activity on a device can be in a running
state: it’s 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.
 The running activity 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.

12
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.
 This is not a typical scenario, because the device’s
screen is usually small, and an activity is either
taking up the whole screen or none at all.
 We often see this case with dialog boxes that come
up in front of an activity, causing it to become
paused.
 All activities go through a paused state to being
stopped.
 Paused activities still have high priority in terms of
getting memory and other resources. This is
because they are visible and cannot be removed
from the screen without making it look very
strange to the user.
 The Activity Manager calls onPause() when putting
your application into the paused state.
13
Stopped state
 When an activity is not visible, but still in memory,
we say it’s in a stopped state.
 A stopped activity could be brought back to the
front to become a running activity again. Or, it
could be destroyed and removed from memory,
which is an operating system choice beyond your
control.
 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,.
 Restarting a stopped activity is far cheaper than
starting an activity from scratch.
 That is because the Activity Manager already has
all the objects loaded in memory and simply has to
bring them all up to the foreground.
 Stopped activities can be removed from memory
at any pointThe Activity Manager calls onStop()
when putting your application into this state, so it
14
is wise in this method to do anything you need in
Destroyed state
 A destroyed activity is no longer in memory.
 The Activity Manager decided that this activity is
no longer needed and has removed it.
 Before 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 destroyed from the destroyed
state.
 It is possible for a stopped activity to be destroyed
as well. For that reason, it is better to do important
work, such as saving unsaved data, in the onStop()
rather than the onDestroy() callback.

15
Intents
 Intents are messages that are sent among the
major building blocks.
 They trigger an activity to start up, tell a service to
start, stop, or bind to, or are simply broadcasts.
 Intents are asynchronous, meaning the code that
sends them doesn’t have to wait for them to be
completed.
 Intents analogy with a website, intents would be
the links connecting various pages together.

16
Contd.
 Intents are messages used for activating
components:
 Intent Object;

Helps identify the receiving component()

May contain action to be taken and data act on

Serve as notification for a system(eg new call)
 Intents can be:
 Explicit: specify receiving component[java class)
 Implicit: the sender specifies just the type of
receiver.

17
Explicit Intent example

18
Implicit Intent example

19
20
Services
 Services run in the background and don’t have any
user interface components.
 They can perform the same actions as activities,
but without any user interface.
 Services are useful for actions that you want to
perform for a while, regardless of what is on the
screen.
 Faceless components that typically run in the
background
 Music player, network download ,etc.
 Services have a much simpler life cycle than
activities (see Figure below).
 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, developers have to be mindful to
run services so that they don’t consume shared
resources unnecessarily, such as the CPU and 21
Service lifecycle

• Just because a service runs in the background doesn’t


necessarily mean it runs on a separate thread.
• By default, services and activities run on the same main
application thread, often called the UI thread.
• If a service is doing some processing that takes a while to
complete (such as performing network calls), you would
typically invoke a separate thread to run it. Otherwise, your 22
Contd.
 Steps to creating a service are:

1. Create the Java class representing your service.

2. Register the service in the AndroidManifest.xml file.

3. Start the service.

public class RefreshService extends Service {…}

 onStartCommand()

Called each time the service is started
 onDestroy()

Called when the service is terminated
 To do that, you can use the Eclipse tool

Source→Override/Implement Methods and select those
three methods.

23
Service example

24
Content Providers
 Content providers are interfaces for sharing data
between applications.
 Enables sharing of data across applications.

Address book, photo gallery, etc.
 Provides API for CRUD operations
 The methods that content providers use to
implement the four critical operations are

25
example

26
Figure. Contacts application using the Contacts
Provider to get the data
Figure. Content provider 27
Broadcast Receivers
 Components designed to respond to broadcast
messages9called Intents)
 Can receive broadcast messages from the system.
For example when:

Anew phone call comes in

There is change in battery level or cellID
 Can receive messages broadcast by Applications

Apps can also define new broadcast Messages
 The receiver is simply dormant code that gets
activated by the occurrence of an event to which
the receiver is subscribed.
 The “event” takes the form of an intent.
 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 completes
booting up, all those events are broadcast, and
any number of receivers could be triggered by
them.
28

 That action is in the form of an intent broadcast.
 When the right intent is fired, the receiver wakes
up and executes.
 The “wakeup” happens in the form of an
onReceive() callback method.

29
Application Context
 So far you have seen activities, services, content
providers, and broadcast receivers. Together, they
make up an application. They are the basic
building blocks, loosely coupled, of an Android
app.
 Think of an application in Android as a container to
hold together your blocks. Your activities, services,
providers, and receivers do the actual work.
 The container that they work in is the shared Linux
process with common Dalvik VM, private file
system, shared resources, and similar things.
 To use our website analogy, an app would be the
website domain.
 Users never really go to Amazon.com (the
domain), but rather visit a web page (which you
could compare to an Android activity) within that
domain, or consume a web service (an
Androidservice). So web pages and web services
are building blocks for a website, and the website 30
31
 The application context is uniquely identified on a
device based on the package name of that
application. For example,
com.marakana.android.HelloWorld would be a
package name of our app.
 There cannot be another app with the same
package name (unless it comes from us, and we
want to use shared user IDs). An application
context gets created whenever the first
component of this application
 You can easily obtain a reference to the context by
calling Context.getApplicationContext() or
Activity.getApplication().

Intent intent = new
Intent(getApplicationContext(),MainActivity.class);
 Keep in mind that activities and services are
already subclasses of the context, and therefore
inherit all its methods.
 Activities and services are also subclasses of the 32

You might also like