Professional Documents
Culture Documents
MAD Chap 5
MAD Chap 5
INTENT
APP
ORIGINATOR
COMPONENT
INTENT ACTION
ANDROID
SYSTEM
USES OF INTENT IN ADNROID
1.Starting an activity:
An Activity represents a single screen in an app.
You can start a new instance of an Activity by passing
an Intent to startActivity().
The Intent describes the activity to start and carries any
necessary data.
For example, if you are having two activities
namely LoginActivity and MainActivity then, you can start
the MainActivity by clicking the login button present on
the LoginActivity.
By using the startActivity(), you can start the desired
Activity using the Intents.
2. Starting a Service
A Service is a component that performs
operations in the background without a user
interface.
You can start a service to perform a one-time
operation (such as downloading a file) by
passing an Intent to startService().
The Intent describes the service to start and
carries any necessary data.
3.Delivering a broadcast
A broadcast is a message that is received by
the application from the system.
The system delivers various broadcasts for
system events, such as when the system
boots up or the device starts charging.
You can deliver a broadcast to other apps by
passing an Intent to sendBroadcast() or
sendOrderedBroadcast().
Intent Types
Activity Lifecycle
For example, when you open your Gmail application, then you see
your emails on your screen. Those emails are present in an Activity. If
you open some particular email, then that email will be opened in
some other Activity.
An activity lifecycle shows all the states with transitioning from one
state to the next state
The Activity lifecycle consists of 7
methods:
onCreate() : It is called when an activity is first created. When
a user opens the app then some Activity is created. You have
to implement this method in every activity because, inside
this method, all the necessary components of your activity
will be initialized. Here the initialization of your application's
UI is done.
onStart(): This method is called when an activity becomes
visible to the user. When all the initialization is done by
the onCreate() method, then this method is called.
onResume(): It is called just before the user starts interacting
with the application. Most of the core functionalities of the
app are implemented in this method.
onPause(): It is called when the activity is paused i.e. it is mostly
called when you press the back or home button of your Android
device. It is an indication that the user is leaving the activity and
starting some other activity.
onStop(): It is called when the activity is no longer visible to the user.
If you are starting a new activity, or some existing activity is entering
into onResume() state, then the current activity will not be visible to
the user and is stopped.
onRestart(): It is called when the activity in the stopped state is about
to start again. By doing so, the state of the activity from the time it
was stopped will be restored.
onDestroy(): It is called when the activity is totally destroyed i.e.
when you clear the application stack then onDestroy() will be called
and all the states of the activity will be destroyed.
Broadcast Lifecycle
Broadcast intents
Custom broadcast intents are broadcast intents that your application sends out.
Use a custom broadcast intent when you want your app to take an action
without launching an activity, for example when you want to let other apps know
that data has been downloaded to the device and is available for them to use.
For example, the following method creates an intent and broadcasts it to all
interested broadcast receivers:
For instance, a Broadcast receiver triggers battery Low notification that you see
on your mobile screen.
Other instances caused by a Broadcast Receiver are new friend notifications,
new friend feeds, new message etc. on your Facebook app.
In fact, you see broadcast receivers at work all the time. Notifications
like incoming messages, WiFi Activated/Deactivated message etc. are all real-
time announcements of what is happening in the Android system and the
applications.
Content provider
<prefix>://<authority>/<data_type>/<id>
Create Content Provider
This involves number of simple steps to create your own content provider.
First of all you need to create a Content Provider class that extends
the ContentProviderbaseclass.
Second, you need to define your content provider URI address which will
be used to access the content.
Next you will need to create your own database to keep the content.
Usually, Android uses SQLite database and framework needs to
override onCreate() method which will use SQLite Open Helper method to
create or open the provider's database.
When your application is launched, the onCreate() handler of each of its
Content Providers is called on the main application thread.
Next you will have to implement Content Provider queries to perform
different database specific operations.
Finally register your Content Provider in your activity file using <provider>
tag.
Methods of content provider
onCreate() This method is called when the provider is
started.
query() This method receives a request from a client. The
result is returned as a Cursor object.
insert()This method inserts a new record into the content
provider.
delete() This method deletes an existing record from the
content provider.
update() This method updates an existing record from the
content provider.
getType() This method returns the MIME type of the data at
the given URI.
Fragments
Each fragment has its own life cycle methods that is affected by activity
life cycle because fragments are embedded in activity.
The application can embed two fragments in Activity A, when running on a tablet-sized
device. However, on a handset-sized screen, there's not enough room for both fragments,
so Activity A includes only the fragment for the list of articles, and when the user selects an
article, it starts Activity B, which includes the second fragment to read the article.
Fragment Life Cycle
Android fragments have their own life cycle very similar to an android
activity. This section briefs different stages of its life cycle.
How to use Fragments
Motion sensors:-
These sensors measure acceleration forces and rotational
forces along three axes. This category includes accelerometers,
gravity sensors, gyroscopes, and rotational vector sensors.
Environmental sensors:-
These sensors measure various environmental parameters,
such as medium air temperature and pressure, illumination,
and humidity. This category includes barometers, photometers,
and thermometers.
Position sensors:-
These sensors measure the physical position of a device. This
category includes orientation sensors and magnetometers.
Sensor Framework
You can access these sensors and acquire raw sensor data by using the Android sensor framework.
The sensor framework is part of the android.hardware package and includes the following classes and
interfaces:
SensorManagerYou can use this class to create an instance of the sensor service. This class provides
various methods for accessing and listing sensors, registering and unregistering sensor event
listeners, and acquiring orientation information. This class also provides several sensor constants that
are used to report sensor accuracy, set data acquisition rates, and calibrate sensors.
SensorYou can use this class to create an instance of a specific sensor. This class provides various
methods that let you determine a sensor's capabilities.
SensorEventThe system uses this class to create a sensor event object, which provides information
about a sensor event. A sensor event object includes the following information: the raw sensor data,
the type of sensor that generated the event, the accuracy of the data, and the timestamp for the
event.
SensorEventListenerYou can use this interface to create two callback methods that receive
notifications (sensor events) when sensor values change or when sensor accuracy changes.
AsyncTask
Audio capture from the device is a bit more complicated than audio and
video playback, but still fairly simple: