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
289
votes
17 answers

How to check if Receiver is registered in Android?

I need to check if my registered receiver is still registered if not how do i check it any methods?
Mikey
  • 4,218
  • 3
  • 25
  • 25
107
votes
3 answers

what is the difference between sendStickyBroadcast and sendBroadcast in Android

What is the difference between sendStickyBroadcast and sendBroadcast in Android?
cobject
  • 1,071
  • 2
  • 8
  • 3
83
votes
3 answers

Android Broadcast Receiver vs Service

I am trying to clarify the difference between a Broadcast Receiver and Service in android. I understand that an activity can start a service by calling startService with an intent. A broadcast receiver can be registered in code or the manifest and…
Chris Muench
  • 17,444
  • 70
  • 209
  • 362
81
votes
1 answer

Should I use android: process =":remote" in my receiver?

I have a BroadcastReceiver which is called every so often, and I have noticed many people use android: process =":remote" in their receiver. Mine is used to check a few things and if the conditions match then activate an alarm. My question is…
Jason
  • 4,034
  • 4
  • 40
  • 62
57
votes
7 answers

Broadcast Receiver Not Working After Device Reboot in Android

I have already checked all the related questions and have not found any solution for this problem. So this is an absolutely new problem for me. What I Have I have an Android app which registers a few broadcast receivers in its manifest. This is what…
53
votes
11 answers

Pop up window over Android native incoming call screen like true caller Android app

I am developing a broadcast receiver for incoming calls in Android and on getting incoming calls I want to inflate a pop up over the native incoming call screen. I completed that code. But now the problem is that in the Android 4.1 (Jelly Bean) API…
39
votes
5 answers

Listen to incoming Whatsapp messages/notifications

I'm working on a notification based app, for which I need to listen to incoming notifications. I've been able to listen to incoming calls, SMS, mail etc. I have no clue how to listen for pings or messages from friends on Whatsapp via code. Can this…
36
votes
3 answers

Fatal Exception: android.app.RemoteServiceException: can't deliver broadcast at android.os.Handler.dispatchMessage

I am using broadcast messages on my android application (From io.socket I am sending broadcast messages to my Activity page). On some devices Samsung SM-G950F and SM-A520F I got an error "Fatal Exception: android.app.RemoteServiceException: can't…
35
votes
6 answers

Android O : PHONE_STATE broadcast limitation

I have been trying to do something similar to truecaller app, where my app is supposed to show a screen after a call gets hung up. Was achieving this by registering android.intent.action.PHONE_STATE implicit broadcast in the manifest file. But it…
32
votes
3 answers

How do I stop hogging the microphone

TL;DR: My app is hogging the user's microphone. Can I turn it off automatically whenever another app needs to use the mic? I have an Android app that has some really cool microphone functionality, similar to Amazon Alexa, that stays on all the time…
Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83
30
votes
4 answers

how to check screen on/off status in onStop()?

as mentioned here, when the screen goes off, the onStop() of current Activity will be called. I need to check the screen on/off status when the onStop() of my Activity is called. so I have registered a BroadcastReceiver for these…
Soheil
  • 1,676
  • 7
  • 34
  • 65
29
votes
1 answer

Differentiate implicit broadcast receiver vs explicit broadcast receiver in the manifest

According to the migration guide to Android O given by Google, most of the implicit broadcast intent should not be registered in the Manifest (minus a few exceptions found here) but explicit broadcast intents remain untouched. We are looking to move…
29
votes
1 answer

Music player control in notification

how to set notification with play/pause, next and previous button in android.! I am new with Android & also at stack overflow. So please bear with me. I set notification when song is start to play like below : ` @SuppressLint("NewApi") public void…
28
votes
2 answers

Android Broadcast Receiver bluetooth events catching

I'm trying to catch bluetooth state changes with Broadcast Receiver. My manifest:
Long Smith
  • 1,339
  • 3
  • 21
  • 40
24
votes
2 answers

Android - Trying to test a service on boot (java.lang.SecurityException: Permission Denial)

I've been trying to test a service when a device boots up on android, but I cannot get it to work. I'm trying to start it with this command from CMD: (in ..\AppData\Local\Android\sdk\platform-tools) adb shell am broadcast -a…
tabache
  • 341
  • 1
  • 2
  • 7
1
2 3
82 83