Broadcast Receiver and Service

Can you explain the following terms: broadcast receiver and service? How do they relate to each other?

By tona2009 asked on 2016-01-07
answer to this question

By shivanishankar01

It makes the service to get and give. Can u please explain me in clear

2016-04-08 Reply

By yuk.dara

- A broadcast receiver (receiver) will receive intents sent by sendBroadcast(). You need to register the broadcast receiver to receive the intents. An intent is an object storing information that can be passed in your application or to other applications. A receiver gets notified by the Android system if the specified event occurs.
To register a broadcast receiver, you need create a sub-class of BroadcastReceiver. You can either dynamically register the instance of this class with Context.registerReceiver() or statically publish an implementation through the <receiver> tag in your AndroidManifest.xml.
Here is an example to receive intents when the screen is off or on.
public class ScreenReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent){
Log.e("Log", "The screen is on.");
Intent intent_meeting_update=new Intent(context,MyWidgetProvider.class);

Log.e("Log", "The screen is off.");

- A Service performs a longer-running operation while not interacting with the user or to supply functionality for other applications to use. Each service class must have a corresponding declaration in its package's AndroidManifest.xml. Services can be started with Context.startService() and Context.bindService().
<service android:name=".ScreenService" />

Here is an example of a Service to to detect screen on and screen off.
public class ScreenService extends Service{
private BroadcastReceiver sReceiver;
public IBinder onBind(Intent arg){
return null;
public int onStartCommand(Intent intent,int flag, int startIs){
// Detect screen off
IntentFilter filter=new IntentFilter(Intent.ACTION_SCREEN_ON);
sReceiver=new ScreenReceiver();
public void onDestroy(){

- The service can communicate with the broadcast receiver to notify an activity. An activity creates a window for you in which you can place your UI with setContentView(View).

2016-01-07 Reply

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.