Services allow applications to perform long-running operations in the background without providing a user interface. Broadcast receivers allow applications to register to receive intents for various system-wide or application events. When a broadcast is sent, all registered receivers are notified. Services run indefinitely until stopped, while broadcast receivers are temporary and only active while handling a broadcast. Both services and broadcast receivers allow applications to perform tasks outside of activities.
5. Android Programming
Android Services
What is a service?
A Service is an application component that can perform long-
running operations in the background and does not provide a user
interface.
A Service can run in the background to perform work even while
the user is in a different application.
A Service runs in the main thread of the application that hosts it,
by default.
5
6. Android Programming
Android Services
What is a service?
This means that, if your service is going to do any CPU intensive
work or blocking operations (such as MP3 playback or
networking), you should create a new thread within the service to
do that work.
6
9. Android Programming
Android Services
Creating a service
Necessary to let the AndroidManifest file know about your
service <service android:enabled="true"
android:name=".DemoService" />
9
10. Android Programming
Android Services
Starting a Service
The application can start the service with the help of
Context.startService method. The method will call the onCreate
method of the service if service is not already created; else
onStart method will be called.
Context.startService() | ->onCreate() – >onStartCommand()
[service running]
10
11. Android Programming
Android Services
Stopping a service
The service started with startService method will keep on running
until stopService() is called or stopSelf() method is called.
context.stopService() | ->onDestroy() [service stops]
11
12. Android Programming
Android Services
Stopping a service
It's important that your application stops its services when it's done
working, to avoid wasting system resources and consuming
battery power.
12
13. Android Programming
Android Services
Service Life Cycle
The lifecycle of a service is much simpler than that of an activity.
However, it's even more important that you pay close attention to
how your service is created and destroyed, because a service can
run in the background without the user being aware.
The service lifecycle—from when it's created to when it's
destroyed—can follow two different paths:
• A started service
• A bound service
13
19. Android Programming
Broadcast Receivers
What is a broadcast receiver?
A broadcast receiver is an Android component which allows to
register for system or application events. All registered
receivers for an event will be notified by Android once this event
happens.
For example Android allows that applications can register for
the ACTION_BOOT_COMPLETED which is fired by the system
once the Android system has completed the boot process.
19
20. Android Programming
Broadcast Receivers
What is a broadcast receiver?
Application 1 Application 2
BroadcastReceiver-1 BroadcastReceiver-1 BroadcastReceiver-3
Filter-2 Filter-1 Filter-2
Service-1 Service-2
Filter-1 Filter-2
20
24. Android Programming
Broadcast Receivers
BroadcastReceiver life cycle?
Get notified when Intent happens
Android Broadcast
System Receiver
Registers for certain intents
24