Questions tagged [android-broadcast]

A broadcast intent on the Android platform is a special intent which is often invoked by the system on an event and catched by a BroadcastReceiver.

A broadcast intent on the Android platform is a special intent which is often invoked by the system on an event and catched by a BroadcastReceiver.

You can either dynamically register an instance of this class with Context.registerReceiver() or statically publish an implementation through the <receiver> tag in your AndroidManifest.xml.

There are following two important steps to make BroadcastReceiver works for the systen broadcasted intents:

  • Creating the Broadcast Receiver.
  • Registering Broadcast Receiver

Creating the Broadcast Receiver

A broadcast receiver is implemented as a subclass of BroadcastReceiver class and overriding the onReceive() method where each message is received as a Intent object parameter.

public class MyReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent intent) {
      Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
   }
}

Registering Broadcast Receiver

An application listens for specific broadcast intents by registering a broadcast receiver in AndroidManifest.xml file. Consider we are going to register MyReceiver for system generated event ACTION_BOOT_COMPLETED which is fired by the system once the Android system has completed the boot process.

<application
   android:icon="@drawable/ic_launcher"
   android:label="@string/app_name"
   android:theme="@style/AppTheme" >

   <receiver android:name="MyReceiver">
      <intent-filter>
     <action android:name="android.intent.action.BOOT_COMPLETED">
      </action>
      </intent-filter>
   </receiver>
</application>

Now whenever your Android device gets booted, it will be intercepted by BroadcastReceiver MyReceiver and implemented logic inside onReceive() will be executed.

See also the documentation about Intents and Intent Filters.

1235 questions
7
votes
2 answers

Volume change Listener: Is registerMediaButtonEventReceiver preferable to onKeyDown?

Looking for a "most comprehensive & compatible (i.e. all Android versions...)" way to listen to volume changes, I found 2 different approaches to handle this: registerMediaButtonEventReceiver onKeyDown + SettingsContentObserver Which method is…
6
votes
4 answers

Not able to receive network related events

I have registered to ConnectivityManager.CONNECTIVITY_ACTION BroadcastReceiver, for receiving network state events, but my onReceive function is not getting called when I turn on or off my wifi connection. As mentioned in the docs, this is an…
User7723337
  • 11,857
  • 27
  • 101
  • 182
6
votes
0 answers

LifeCycleOwner in Broadcast reciever

I am using Android Room to store my application data. After device reboot I need to retrieve them and do some functions. I am using LiveData for getting data from database. But I cannot bind Broadcast Reciever as owner. How should I resolve this…
voximdo
  • 53
  • 2
  • 14
6
votes
2 answers

Broadcast action for WIFI change

In my application I have to get notified whenever the device connects or disconnects from a WIFI network. For this I have to use a BroadcastReceiver but after reading through different articles and questions here on SO I'm a bit confused which…
Cilenco
  • 6,951
  • 17
  • 72
  • 152
6
votes
1 answer

Prevent change of order of application presentation Android after intent service launch

I have the following situation: One main application is the launcher application (A), always the application is running, and this application call another a child application (B) fig 1 . The problem occurs when the application (A) launch an intent…
6
votes
1 answer

Android Wifi Scan - BroadcastReceiver for SCAN_RESULTS_AVAILABLE_ACTION not getting called

Here is my code: public class FloatWifiManager implements IWifiManager { private WifiManager wifiManager; private BroadcastReceiver wifiScanReceiver; public FloatWifiManager(Context context) { ... wifiManager =…
6
votes
5 answers

Communicate between different instances of same fragment

The problem as follows. Let us have 3 tabs with fragments: Tab 1 (Fragment A). Needs to send data to Tab 2. Tab 2 (Fragment B). Needs to receive data from Tab 1. Tab 3 (Fragment B). Already contains data. As you see Tab 3 and Tab 2 contain the…
AnZ
  • 1,040
  • 24
  • 54
6
votes
3 answers
6
votes
1 answer

How does Starbucks show a UI on Android devices when near store?

I am a fairly new Android developer. One thing that I noticed is that when I walk near/inside a Starbucks store, my Android device shows the following: Question: How does this UI render automatically onto my mobile device when I am near/inside the…
code
  • 5,294
  • 16
  • 62
  • 113
6
votes
1 answer

Android Lollipop Sends Multiple BroadcastReceivers for Telephone State Changes

Up to android kitkat phone state broadcast receiver works fine. In android lolipop phone state broadcast receiver sending multiple broadcast. Is there any thing changed in Android Lolipop. public class PhoneStateBroadcastReceiver extends…
Kamalanathan
  • 1,747
  • 3
  • 22
  • 33
6
votes
1 answer

Android broadcast to specific 3rd party applications

I developed an android app and it sends broadcast with a custom permission. public abstract void sendBroadcast (Intent intent, String receiverPermission) Now any 3rd party app that has a broadcast receiver and declared the custom permission in…
pdiddy
  • 6,217
  • 10
  • 50
  • 111
6
votes
1 answer

How to get sms sent confirmation for each contact/person in android?

I want to send sms to multiple people and verify whether sms sent or not. I checked multiple links (mentioned here) and got the idea of using PendingIntent and broadCast Receiver for confirmation. Practical way to find out if SMS has been…
RStar
  • 100
  • 2
  • 7
6
votes
1 answer

Android calendar BroadcastReceiver host : com.android.calendar vs com.google.android.calendar

After looking around a bit, I've noticed there are 2 possible hosts for the Android calendar. The first is com.android.calendar and the other is com.google.android.calendar. I'm pretty sure it reflects the new standalone calendar Google released…
SagiLow
  • 5,721
  • 9
  • 60
  • 115
6
votes
1 answer

Call broadcast receiver at time of uninstalling application in android

I want to clean up the junk created by my application at time on UnInstalling the Application. Using ManiFest File:- Added in Manifest File:
Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
6
votes
2 answers

Listen for new calendar events

Mainly what I need is to react depending on the events in the default android calendar (Google). It's fairly easy to query the provider and get the events at any given time but my data has to be consistent all the time. What I want is to avoid…
MariusBudin
  • 1,277
  • 1
  • 10
  • 21