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
16
votes
5 answers

Android ACTION_DATE_CHANGED broadcast

I have a Nexus S, and when I change the date manually on the phone, ACTION_DATE_CHANGED is not always broadcasted. If I change the date from Feb 13, 2014 to Feb 14, 2014, I have not gotten an ACTION_DATE_CHANGED to work, but if I set it to several…
ZachM
  • 893
  • 2
  • 8
  • 22
16
votes
1 answer

How do I implement a BroadcastReceiver in a Service Class in Android?

I need to implement BroadcastReceiver in a Service class I have created: public class MyService extends Service In this class I have to implement a simulation of download by using a Thread - Sleep when the user presses the button in the MyActivity…
Dilshad Abduwali
  • 1,388
  • 7
  • 26
  • 47
14
votes
2 answers

Android 8.0 Oreo AlarmManager with broadcast receiver and implicit broadcast ban

I have critical reminders that are set via the Alarm Manager (It should function the same way as an alarm clock application). Previously I had the following in my Android Manifest:
ViciDroid
  • 3,555
  • 4
  • 14
  • 16
14
votes
2 answers

Access to application class in Broadcast Receiver

I want to check internet connection in Broadcast Receiver; And set result (A boolean flag) to a global variable, to use it on whole application, in if conditions; That if internet is disconnected, set a status imageview in main activity, to red…
Dr.jacky
  • 3,341
  • 6
  • 53
  • 91
13
votes
1 answer

Local broadcast from Service not received by Activity

I have an Activity in which I am registering a BroadcastReceiver locally as follows: public class SomeActivity extends Activity{ public static final String PERFORM_SOME_ACTION = "PERFORM_SOME_ACTION"; @Override protected void…
13
votes
3 answers

Alarm Manager does not work in background on Android 6.0

This is my Activity code, Long time = new GregorianCalendar().getTimeInMillis()+20000;//Setting alarm after 20 sec Intent intentAlarm = new…
12
votes
1 answer

Unable to wake up app from background on Xiaomi devices till "Autostart" is enabled manually

On Xiaomi's MI devices, there is a feature of turning off/on "Autostart" in their security app. (In Security App->Permissions->AutoStart) This means none of the broadcast receivers receive anything while the app is not running. So BOOT_COMPLETED,…
12
votes
3 answers

Android: Broadcast ACTION_MY_PACKAGE_REPLACED never received

My app runs a service which is terminated when the device reboots or the app is reinstalled (updated). I've added two broadcast receivers to catch those events - BOOT_COMPLETED and ACTION_MY_PACKAGE_REPLACED. The ACTION_MY_PACKAGE_REPLACED receiver…
Scott
  • 3,663
  • 8
  • 33
  • 56
11
votes
1 answer

Listen for screenshot action in android

I am new to android development. I am making an application that will react when screenshot of android phone is taken. I have heard that android allows such actions can be detected by Broadcast Receivers , so I have gone through android developer…
droidev
  • 7,352
  • 11
  • 62
  • 94
11
votes
1 answer

AlarmManager does not work when app is force closed

Documentation for AlarmManager startes that Note: The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running. For normal timing operations (ticks,…
tony9099
  • 4,567
  • 9
  • 44
  • 73
10
votes
2 answers

How to find the package name which has been uninstalled when using Intent.ACTION_PACKAGE_REMOVED

I have an application which is keeping a log of internally developed applications installed on the device. Upon installation a broadcast receiver for Intent.PACKAGE_ADDED is invoked and records the package name using the following code: public class…
10
votes
3 answers

How to fix the Android error "Background execution not allowed: receiving Intent {...}"

So I'm programming an Android app that uses Bluetooth discovery of devices. Here is the code I use to start discovery. try { myBluetoothAdapter.startDiscovery(); Log.d("Bluetooth Started successfully","yes"); } catch (Error e) { …
10
votes
0 answers

android.app.RemoteServiceException "can't deliver broadcast"

My app does not uses broadcasts - neither using Context nor using LocalBroadcastManager. Since I use some external libraries inside the app source code, it might be possible that one of them uses broadcasts inside them. I can see lots of occurrences…
10
votes
4 answers

startActivity not working when calling from BroadcastReceiver

I have a notification and when I select, it sends a Broadcast to a BroadcastReceiver using a PendingIntent. In the onReceive I start a new Activity. However, if I remove my app from recent apps opened (or the notification sit in the draw for a long…
10
votes
3 answers

Abort SMS Intent on Android KitKat

I'm currently developing an application that needs to deal with SMS only if it is an SMS expected by the application (same behaviour as Whatsapp on registration). I would like to abort the SMS Intent as it is not expected to appear in the SMS…
user2058839
1 2
3
82 83