Android Broadcast Receivers with Example

What are Broadcasts?

Consider a situation in which you are downloading a large file in your app and suddenly the battery goes critically low , you don’t want the download to stop abruptly and would like to show the user a notification/dialog so that he can pause the download.  Creating a service just to keep on checking the battery power would be too much work for a simple notification. What if the android system itself notifies you of any such system event like battery low or airplane mode.

Guess what?  It does!. And you can handle these events very efficiently.. These events are known as broadcasts and you need to handle them in a broadcast receiver. Your app can have its own custom broadcasts which it can send within the app or to other apps on the device to notify them of an event they might be interested in.

One common use case of  sending broadcasts within the same app is mentioned here:

Suppose you have updated your  profile details in an activity inside the app, you would like the updated changes to be reflected immediately in all the other active activities without having to refresh them. In this case you can send a broadcast informing the other activities that the profile details are updated

Continue reading

BroadcastReceiver in Android | With Example

BroadcastReceiver is one of the four basic Android Components. In this example we will be learning how to create a BroadcastReceiver and listen for broadcast. Before that let us first understand what are Broadcasts

What are Broadcasts?

Consider a situation in which you are downloading a large file in your app and suddenly the battery goes critically low , you don’t want the download to stop abruptly and would like to show the user a notification/dialog so that he can pause the download.  Creating a service just to keep on checking the battery power would be too much work for a simple notification. What if the android system itself notifies you of any such system event like battery low or airplane mode.

Guess what?  It does!. And you can handle these events very efficiently. These events are known as broadcasts and you need to handle them in a broadcast receiver. Your app can have its own custom broadcasts which it can send within the app or to other apps on the device to notify them of an event they might be interested in.

One common use case of  sending broadcasts within the same app is mentioned here:

Suppose you have updated your  profile details in an activity inside the app, you would like the updated changes to be reflected immediately in all the other active activities without having to refresh them. In this case you can send a broadcast informing the other activities that the profile details are updated

Continue reading

Android Service-LifeCycle and Working | With Example

 

Ever wondered how a song that you started on your music app plays even after you have killed the app? Well, this happens because of service. Service is a indefinitely running background operation without an UI .Google explains service as

A Service is an application component that can perform long-running operations in the background, and it does not provide a user interface. Another application component can start a service, and it continues to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service can handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.

Continue reading

Android Components:Four pillars of every Android app

Before beginning with the actual app development it is very important for every developer to be aware of the four Android Components. These are building blocks of an Application. Any feature that we see in an app is the result of interaction between these components.A developer needs to declare all the components he is using inside the AndroidManifest.xml, this what informs the system of number and names of components used in this app.

The four components are

  • Activity
  • Service
  • Content providers
  • Broadcast Receivers

Continue reading

Android Components : Four pillars of every Android app

Before beginning with the actual app development it is very important for every developer to be aware of the four Android Components. These are building blocks of an Application. Any feature that we see in an app is the result of interaction between these components. A developer needs to declare all the Android components he is using inside the AndroidManifest.xml, this what informs the system of number and names of components used in this app.

The four components are

  • Activity
  • Service
  • Content providers
  • Broadcast Receivers

Continue reading