Service versus BroadcastReceiver

4

What's the difference, on Android, between Service and BroadcastReceiver?

  • How long can a Service run (running)?
  • How long can a BroadcastReceiver run (running)?
  • Can I create notifications via BroadcastReceiver or just via Service?
  • Is it a good practice (the previous question)?
asked by anonymous 07.04.2016 / 17:19

2 answers

3

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 component of the application can start a service and it will continue to run in the background even if the user switches to another application. In addition, a component can bind to a service to interact with it and even establish interprocess communication (IPC). For example, a service can handle network operations, play music, perform file I / O, or interact with a content provider, all from the background.

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

How long can a Service run (run)?

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

When started through bindService () it will be running as long as there are linked clients.

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

How long can a BroadCastReceiver run (run)?

A BroadcastReceiver is started by the OS if it is eligible for the event being 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 object complete and no longer active and can be destroyed by the OS.

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

Can I create notifications via BroadCastReceiver or just via Service?

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

You can do everything you want in the onReceive () since it is synchronously (1) .
Because this code runs in the main thread do not use them for long processes.

Is it a good practice (the previous question)?

Yes, as I said earlier, posting notifications is a usual way to use BroadcastReceiver.

(1) This limitation can be avoided used registerReceiver (BroadcastReceiver receiver, IntentFilter filter, String broadcastPermission, Handler scheduler) / p>     

08.04.2016 / 13:16
0

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

As for the rest:

  • For as long as it takes. The service can be created by the application and run continuously indefinitely, even when it is stopped (when it is open but you are running another application);
  • Also indefinitely. You can register one by one Activity , in which case it only runs while the activity is running, or by the application manifest, in this case it runs independently of the activity;
  • As the name itself indicates BroadCastReceiver ("Broadcast Receiver") is used to receive notifications and will not send them;
  • It does not make sense; (.
  • 07.04.2016 / 19:25