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

How to send a custom broadcast action to receivers in manifest?

MyReceiver.java public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, final Intent intent) { Log.i("MyReceiver", "MyAction received!"); } } In AndroidManifest.xml (under the…
Yairopro
  • 9,084
  • 6
  • 44
  • 51
23
votes
4 answers

Android - How to trigger a Broadcast Receiver to call its onReceive() method?

I have scheduled alarm for my application. I have implemented broadcast receiver to be triggered once the alarm time reaches. How to manually call broadcast receiver to execute the code inside of onReceive method without replicating the code…
22
votes
5 answers

Dynamic Registration vs Static Registration of BroadcastReceiver

All of us known we register BroadcastReceiver in two types 1)Static Registration 2)Dynamic Registration But my doubt is when we need to use Static and when we need to use Dynamic?
Krishna
  • 343
  • 1
  • 2
  • 9
22
votes
3 answers

Listen to own application uninstall event on Android

As far as I know, apps can't get intents for their own uninstallation: ACTION_PACKAGE_FULLY_REMOVED ACTION_PACKAGE_REMOVED But how does Dolphin Browser manage to receive a "removed" event and start a browser as in the attached image? ADB: …
NitZRobotKoder
  • 1,046
  • 8
  • 44
  • 74
21
votes
1 answer

Can you use pending intents with localbroadcasts?

I am interested in using pending intents with local broadcasts. To make myself clear, I am using the following for registering receivers and sending broadcast: android.support.v4.content.LocalBroadcastManager. I have a local broadcast receiver in a…
Nims
  • 477
  • 1
  • 5
  • 12
20
votes
3 answers

Is it good to replace broadcast receiver with Greenrobot Eventbus for triggering event based functions and data transfer from service to activity?

I have implemented a service, where I handle the state changes(connect, disconnect, onServiceDiscoverd, onCharacteristicChange etc) and receiving data from another device through gatt Server. My question is, Can the events be handled efficiently…
20
votes
3 answers

Android: are context-registered broadcast receivers exported?

If I register a broadcast receiver say in my activity like this, @Override protected void onResume() { super.onResume(); myReceiver = new BroadcastReceiver() { ... }; IntentFilter filter = new IntentFilter("com.example.MY_ACTION"); …
Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
18
votes
5 answers

android.intent.action.BOOT_COMPLETED Intent is not received at "Restart" or "Reboot"

Android android.intent.action.BOOT_COMPLETED Intent is not received if I use the "Restart" or "Reboot", but works if I turn off and on the device. Is there Any way to make this work?
TibiG
  • 819
  • 1
  • 6
  • 18
18
votes
5 answers

Android Services: START_STICKY does not work on Kitkat

I am using services in an application to listen for how many times the user presses his/her power button. The implementation was working fine on all devices. But when I tested the app on the Android Kitkat, I noticed something wrong. As soon as I…
Rakeeb Rajbhandari
  • 5,043
  • 6
  • 43
  • 74
17
votes
8 answers

Why SMS Retriever API don't work in release mode?

I've implemented the SMS Retriever API like in the google tutorials and in my debug Build Variant work fine. I can read the sms and get the code to the user can do the login. My problem is when I run the app in release Build Variant the sms it…
17
votes
3 answers
17
votes
3 answers

Broadcast Receiver class and registerReceiver method

Hi i am trying to understand Broadcast Receiver , i went through many sample codes , but still have some doubts. I wanted to know when we have to extend the Broadcast Receiver class and when should we use registerReceiver() method and when should we…
user2717079
  • 299
  • 1
  • 3
  • 13
17
votes
4 answers

Android broadcast receiver not receiving intent

I have two apps that I made, and am trying to send an intent from one to the other but the intent is never getting to the onReceive() however this problem is only one way. The first app can send to the second but the second cannot send back info. I…
Cob50nm
  • 911
  • 2
  • 9
  • 27
17
votes
2 answers

Best way to communicate a service with an activity (broadcast, callbacks, etc)

What I have: I have a library running on a process using aidl. I have an app that uses this library, and on the messaging activity, I connect with the service to send messaging and I have a broadcast receiver to manage the incoming messages. The…
Pipeline
  • 567
  • 1
  • 8
  • 23
16
votes
3 answers

java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED on KitKat only

I am using the DownloadManager to download images off our server and I am placing the files in the externalFilesDir. I am send out a broadcast intent because I don't want these downloaded images to appear in the gallery. sendBroadcast(new…
The Nomad
  • 7,155
  • 14
  • 65
  • 100
1
2
3
82 83