0% found this document useful (0 votes)
7 views

lecture4

The document outlines the essential components of an Android application, including Activities, Services, Broadcast Receivers, and Content Providers, each serving distinct functions. Activities provide user interfaces, Services run background operations, Broadcast Receivers handle messages from other applications, and Content Providers facilitate data sharing between apps. Additionally, it discusses the component lifecycle and the use of Intents and Intent Filters for communication and interaction within the application.

Uploaded by

bushrachoohan786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

lecture4

The document outlines the essential components of an Android application, including Activities, Services, Broadcast Receivers, and Content Providers, each serving distinct functions. Activities provide user interfaces, Services run background operations, Broadcast Receivers handle messages from other applications, and Content Providers facilitate data sharing between apps. Additionally, it discusses the component lifecycle and the use of Intents and Intent Filters for communication and interaction within the application.

Uploaded by

bushrachoohan786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

LECTURE:4

ANDROID COMPONENTS
ANDROID APPLICATION COMPONENT
Application components are the essential building blocks
of an Android application.
These components are loosely coupled by the application
manifest file AndroidManifest.xml that describes each
component of the application and how they interact
There are following four main components that can be
used within an Android application −
ACTIVITIES

An activity represents a single screen with a user


interface,in-short Activity performs actions on the screen.
For example, an email application might have one activity
that shows a list of new emails, another activity to compose
an email, and another activity for reading emails. If an
application has more than one activity, then one of them
should be marked as the activity that is presented when the
application is launched.
An activity is implemented as a subclass of Activity class as
follows −
public class MainActivity extends Activity {
}
ACTIVITY
Activities provide a user interface for one specific task, Basic
component of most applications
Most applications have several activities that start each other as
needed
Each is implemented as a subclass of the base Activity class.
• presentation to the user
o full-screen windows
o floating windows
o embedding inside of another activity
SERVICES

A service is a component that runs in the background to


perform long-running operations. For example, a service
might play music in the background while the user is in a
different application, or it might fetch data over the
network without blocking user interaction with an
activity.
A service is implemented as a subclass of Service class
as follows −
public class MyService extends Service {
}
SERVICE

Services execute background processing, no visual interface


Ex: Downloads, Playing Music, TCP/UDP Server
You can bind to an existing service, control its operation, and
run in background
Play music, alarm clock, etc.
Secured if using permissions
Callers may need to verify that service is the correct one
BROADCAST RECEIVERS

Broadcast Receivers simply respond to broadcast messages


from other applications or from the system. For example,
applications can also initiate broadcasts to let other
applications know that some data has been downloaded to the
device and is available for them to use, so this is broadcast
receiver who will intercept this communication and will
initiate appropriate action.
A broadcast receiver is implemented as a subclass
of BroadcastReceiver class and each message is broadcaster
as an Intent object.
public class MyReceiver extends BroadcastReceiver { public
void onReceive(context,intent){} }
BROADCAST RECEIVERS

Broadcast receivers act as mailboxes for messages from


other applications. It receives and reacts to broadcast
announcements
If an app registered the receiver in adv., the event will
notify and call back the registered software
Ex: Low battery, power connected, shutdown, timezone
changed, etc.
BROADCAST RECEIVERS

Act as receivers for multiple components


Provide secure IPC
Done by specifying permissions on BroadcastReceiver
regarding sender
Otherwise, behave like activities in terms of IPC
⚫ Can’t define permission
Don’t send sensitive data
CONTENT PROVIDERS

A content provider component supplies data from one


application to others on request. Such requests are
handled by the methods of the ContentResolver class.
The data may be stored in the file system, the database or
somewhere else entirely.
A content provider is implemented as a subclass
of ContentProvider class and must implement a
standard set of APIs that enable other applications to
perform transactions.
public class MyContentProvider extends
ContentProvider { public void onCreate(){} }
CONTENT PROVIDER

Content providers are data storage facilities which


supports data exchange between applications
Make data available to other applications
Transfer data between applications in Android
Other applications use a ContentResolver object to
access the data provided via a ContentProvider
CONTENTPROVIDERS

Generally SQL backend


Used to share content between apps
Access controlled through permission tags
Apps can be dynamically authorized access control
⚫ Possible security hole
Must protect against SQL injection
⚫ verify input using parameterization
COMPONENT LIFE CYCLE

Activities
⚫ Can terminate itself via finish();
⚫ Can terminate other activities it started via
finishActivity();
Services
⚫ Can terminate via stopSelf(); or
Context.stopService();
Content Providers
⚫ Are only active when responding to ContentResolvers
Broadcast Receivers
⚫ Are only active when responding to broadcasts
ADDITIONAL COMPONENTS
INTENTS
An intent is an Intent object with a message content.
Activities, services and broadcast receivers are started by
intents. ContentProviders are started by
ContentResolvers:
⚫ An activity is started by Context.startActivity(Intent intent) or
Activity.startActivityForResult(Intent intent, int RequestCode)
⚫ A service is started by Context.startService(Intent service)
⚫ An application can initiate a broadcast by using an Intent in any
of Context.sendBroadcast(Intent intent),
Context.sendOrderedBroadcast(), and
Context.sendStickyBroadcast()
INTENT FILTERS
Declare Intents handled by the current application (in the
AndroidManifest):

<?xml version="1.0" encoding="utf-8"?> Shows in the


<manifest . . . > Launcher and
<application . . . >
<activity android:name="com.example.project.FreneticActivity" is the main
android:icon="@drawable/small_pic.png" activity to
android:label="@string/freneticLabel"
... > start
<intent-filter . . . >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter . . . >
<action android:name="com.example.project.BOUNCE" />
<data android:mimeType="image/jpeg" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
... Handles JPEG
</application>
</manifest> images in
some way

You might also like