Service versus Broadcastreceiver

Asked

Viewed 498 times

4

What’s the difference, on Android, between Service and Broadcastreceiver?

  • How long a Service can run (running)?
  • How long can a Broadcastreceiver run (running)?
  • I can create notifications via Broadcastreceiver or only via Service?
  • It is good practice (the previous question)?

2 answers

4

The documentation defines Service as follows:

A Service is an application component that can perform long operations and does not provide a user interface. Another application component can launch a service and it will continue running in the background even if the user switches to another application. In addition, a component may link to a service to interact with it and even establish inter-process communication (IPC). For example, a service can handle network operations, play music, run I/O files, or interact with a content provider, all from the background.

In turn, a Broadcastreceiver is a component that allows you to be registered to host events.
All receivers registered for an event are notified by the Android OS once this event happens.

How long a Service can run (running)?

When initiated through startService() the service will remain running until interrupted on own account with stopSelf() or by the component that started it, calling stopService().

When initiated through bindService() it will run as long as there are connected clients.

A service can be destroyed by the OS when the memory is low and needs to recover system resources.

How long can a Broadcastreceiver run (running)?

A Broadcastreceiver is initiated by the OS if it is eligible for the event launched.

A Broadcastreceiver is only valid for the duration of the method call Onreceive()
Once the code returns from this method, the system considers the completed object no longer active and can be destroyed by the OS.

The documentation says that after 10 seconds the receiver will be blocked and may be destroyed.

I can create notifications via Broadcastreceiver or only via Service?

If you are referring to notifications (Notificationmanager) that appear in the notification area of the device, yes, it is up to a usual way of using Broadcastreceiver.

You can do anything you want in the method onReceive() provided that it is synchronous(1).
How that code runs on main thread do not use them for long processes.

It is good practice (the previous question)?

Yes, as I said earlier, launching notifications is a common way of using Broadcastreceiver.

(1)This limitation can be avoided used registerReceiver (Broadcastreceiver receiver, Intentfilter filter, String broadcastPermission, Handler Scheduler)

1

A service is a component that performs long-term actions in the background, it does not present a user interface. As an example you could create a service that accesses a certain external API in search of new data, when realizing that there are updates your service could make a broadcast of the data using sendBroadcast (method of a Parent class), at which point the BroadcastReceiver, which is the class used to "listen" to Broadcasts generated by calls from sendBroadcast.

For the rest:

  1. For as long as it takes. The service can be created by the application and run continuously indefinitely, even with it stopped (when it is open but you are running another application);
  2. Also indefinitely. You can register one by one Activity, in this case it only executes while Activity executes, or then by the application manifest, in which case it executes independently from Activity;
  3. As its name implies BroadCastReceiver ("Receiver of Broadcasts") is used to receive notifications and does not send them;
  4. Makes no sense ;(.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.