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
6
votes
3 answers

Are there any advantages to switch to Otto from Broadcast events

I stumbled upon Otto, and it looks like it's used as a replacement for Broadcast events. I read the doc but, I don't understand if there are much advantages to use Otto.
6
votes
3 answers

android broadcastreceiver vs listeners

If you have a Service that frequently (every second or half a second) sends updates, are there pros/cons to using Broadcasts vs registering Listeners (an Interface you create) that get stored in some sort of List in the Service and sending out…
StackOverflowed
  • 5,854
  • 9
  • 55
  • 119
6
votes
2 answers

Broadcast receiver push notification

I have implemented push notification in my android application: In my main class: // PUSH Parse.initialize(this, applicationId, clientKey); PushService.setDefaultPushCallback(this,…
6
votes
2 answers

Push Notification receiver android( manifest?)

I am using google push notifications, it worked perfectly until now, At the moment the push notifications doesnt work beacause onRegistered is never launched. I think the problem is in the manifest(since the problem is that the receiver is never…
6
votes
3 answers

How to trigger MediaScan on Nexus 7?

Because I want to make sure the MediaStore has the latest information without having to reboot I'd like to trigger the MediaScanner using the popular way I found on SO context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, …
Bernd S
  • 1,278
  • 1
  • 11
  • 18
6
votes
2 answers

WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION is never fired

I am registering receiver on onResume(): registerReceiver(wifiConnectivityReceiver, new IntentFilter(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)); This is the receiver itself : class WiFiConnectivityReceiver extends…
EvZ
  • 11,889
  • 4
  • 38
  • 76
6
votes
2 answers

Can't reinstall crashed Sony SmartWatch Control

I hope this is not to generic but I am developing an app for the Sony SmartWatch. When ever I make a mistake like allowing a null pointer exception. I can not get my app to restart. It's like it stays in the crashed state forever. To make the…
theJosh
  • 2,894
  • 1
  • 28
  • 50
6
votes
0 answers

How to detect Auto Sync state changed via broadcast receiver

Currently I am working on the widget that has several toggle features, and one of them is Auto-Sync toggle. I managed to implement enabling and disabling of the Auto-Sync using flowing code. public class AutoSyncUtility { private static final…
Zivorad Baralic
  • 376
  • 2
  • 7
5
votes
0 answers

Android sendBroadcast to other module

I have a main application module "app1" and a second module "app2" that starts as "process:SecondActivity". I need to send a message from SecondActivity using a broadcast receiver, but the message does not come in onRecive. Broadcast receiver is in…
5
votes
2 answers

Widget issue: BroadcastQueue: Background execution not allowed: receiving Intent

My app widget stops working after upgrading to targetSDk to 28. It is flawlessly working on old targetsdk devices. I am getting the following error: W/BroadcastQueue: Background execution not allowed: receiving Intent {…
5
votes
0 answers

Delayed broadcast response for new SMS Retriever API

Recently we decided to start using SMS Retriever API to read OTP. I followed all steps and it's working correctly. The problem is BroadcastReceiver is getting callback after a long delay. SMS notification is pushed by system as soon as SMS is…
5
votes
1 answer

Manifest-declared BroadcastReceiver not picking up explicit broadcast when app is not running

I am trying to get two apps to communicate via broadcasts. The first app sends a broadcast using code like the following: Intent outIntent = new Intent("org.example.WHATEVER"); PackageManager pm = this.getPackageManager(); List
user149408
  • 5,385
  • 4
  • 33
  • 69
5
votes
0 answers

Receive Callback / Broadcast when pinning shortcut in android O

I am trying to add two shortcuts at once. But android 8 shows a dialog and requests permission from the user to add shortcut and two of those dialogs cannot be presented at once. So I need a way by which I can get a callback or broadcast when the…
5
votes
4 answers

Android BroadcastReceiver for internet connection called twice

I am facing this issue for a while and noticed that a few other people also faced it, but I am not able to resolve it. The Problem I have a BroadcastReceiver, which keeps listening for the internet changes. So whenever the internet is connected or…
5
votes
4 answers

Oreo: Broadcast receiver Not working

I was trying to get notification trigger on my application whenever the user makes a new call. I'm registering receiver in my activity and destroying it in onDestroy() method. Following is the code snippets for registering…