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

BroadcastReceiver not working when app is not running

In my manifest file I have declared the receiver. (as follows) however, once I shut down my application, I am not able to get the alarms and the notifications. Apparently, a call to the OnReceive in my…
8
votes
3 answers

How to get Category of application using package name

I am having two or more application having my specified category like and I am able to get all packages having this category by : final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); …
7
votes
2 answers

Detecting incoming and outgoing calls in android API29+

There are many questions about detecting incoming and outgoing calls in android, but all of them are old and also android deprecates the useful functions, and google play rejects my application because I'm using them. For detecting outgoing calls I…
7
votes
4 answers

Background Service Stop when removing app from recent

Background service is stop, when removing my app from recent in oppo & vivo mobiles, & Broadcast reciever also not working in that case.
7
votes
2 answers

How to receive USB connection status broadcast?

I am trying to detect USB connection in my app, that is, whether or not USB is connected to device. It's being tested on Marshmallow 6.0.1 (sdk23) But I'm unable to receive the broadcast actions ACTION_USB_DEVICE_ATTACHED or…
7
votes
1 answer

Android BroadcastReceiver in Service giving null pointer exception when sending broadcast to it

I have a broadcast receiver in my service, and when I send a broadcast to it from my activity to stop the service for example, I get the following error: java.lang.NullPointerException: Attempt to invoke virtual method 'void…
Cyogenos
  • 3,509
  • 3
  • 15
  • 14
7
votes
2 answers

Android OrderedBroadcast Not Working In Release Build

Why would an OrderedBroadcast work in the debug build of the app but not in release? I am sending the following OrderedBroadcast: context.sendOrderedBroadcast(sendInt, "xxx.xxxx.permission.API", new BroadcastReceiver() { …
7
votes
0 answers

Android BLE Scan in Background Service

I'm trying to make an Android app that will scan for a certain Bluetooth device as a background service. Once the phone is within a certain range of the Bluetooth device, measured by reading the RSSI, the background service will start an Activity…
7
votes
2 answers

How can send sms in android in dual SIM?

How can send sms in android with dual sim for broadcast not intent?? How can detect dual sim in android? User wants to select sim for send sms broadcast. In android set the default sim for sending msg in dual sim in android. Thanks, Nitin
user3315464
7
votes
7 answers

How to keep service running in background

I am creating an app that launches an activity as soon as the user presses the power button 3 times. After some research I found out that to do this, first you need to create a service which launches a broadcast receiver to check the screen's on/off…
user3619483
7
votes
2 answers

Difference between Intent.ACTION_REBOOT and Intent.ACTION_SHUTDOWN

I am trying to receive (with BroadcastReceiver) the action of shutting down, booting and rebooting. I searched a lot, but I could not figure out what is the difference between Intent.ACTION_REBOOT and Intent.ACTION_SHUTDOWN, and when is…
Or B
  • 1,675
  • 5
  • 20
  • 41
7
votes
2 answers

Android start service using broadcast receiver

I am implementing the following code, in which I want to start a service using broadcast receiver. The toast in the broadcast receiver is working fine but the service is not executing. Can anyone tell me where I went wrong? MyReceiver.class public…
Spike
  • 71
  • 1
  • 1
  • 2
7
votes
2 answers

Can't cancel Bluetooth discovery process

I need to do a scan of bluetooth devices in the surrounding area for 6 to 12 seconds. After this time I need to stop the discovery of new devices. The following code should: Start scanning for bluetooth devices Print out any which are found After 6…
TomSelleck
  • 6,706
  • 22
  • 82
  • 151
7
votes
2 answers

How to fire an event like launch an application when a text is selected

I was wondering if i can launch an activity or application when a text is selected in any application like browser, messages etc. Like when we select a text at any where a small pop-up appears mentioning cut, copy, paste option. can i add another…
Saqib Vohra
  • 366
  • 7
  • 17
7
votes
3 answers

BroadcastReceiver for Screen On/Off not working

I am trying to use BroadcastReceiver but it is not working, please help me to solve this problem. MyReceiver.java package com.example.broadcast_receiver; import android.content.BroadcastReceiver; import android.content.Context; import…
user2290872
  • 95
  • 1
  • 1
  • 5