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

Refresh the Gallery after deleting an image file?

I always found the following answer for my Question: context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); but it do not work on my System (Nexus4 Android 4.…
user2184013
  • 101
  • 1
  • 4
10
votes
5 answers

How to check user entered own phone number in EditText?

Mobile number will be entered in an edittext by user on registration page in my Android application. How can I check that user entered his/her mobile number not other's ? I've tried this : TelephonyManager tMgr…
Manish Dubey
  • 4,206
  • 8
  • 36
  • 65
9
votes
1 answer

How to differentiate the connected bluetooth device in android programmatically?

Whether it is a bluetooth headset or mobile phones? how to differentiate the bluetooth headset and bluetooth enabled android device in android code. I am developing a small application,in that I have a feature of blocking the data transfer via…
9
votes
1 answer

Equivalent of LocalBroadcastManager without Android support library

There are lots of examples on StackOverflow and elsewhere that use the class LocalBroadcastManager to avoid broadcasting events outside of an app. However, this class uses the Android support library as shown in the package name:…
Vincent Hiribarren
  • 5,254
  • 2
  • 41
  • 65
9
votes
6 answers

How to make the app's broadcast receiver keep listening without having a service running in the background

I'm trying to make an android application which listens to wifi change broadcast and do some action. But the problem is that it is not working when the process is killed. I found this question which says that it won't work without an activity How to…
Alex
  • 1,178
  • 3
  • 9
  • 24
9
votes
4 answers

Android , broadcasting parcelable data

I've implemented a class that extends NotificationListenerService which works fine for picking up on notifications posted. I'm then wanting to take the statusBarNotification object received and broadcast it. I'd doing the following: @Override public…
Andrew
  • 7,548
  • 7
  • 50
  • 72
8
votes
2 answers

Listen incoming calls through BroadcastReceiver, without PhoneStateIntentReceiver or PhoneStateListener

Is there any way to listen to incoming calls by extending BroadcastReceiver to listen to OS's broadcast,without using PhoneStateIntentReceiver or PhoneStateListener. Also please tell me what will be action and permissions in manifest. I've tried…
CR Sardar
  • 921
  • 2
  • 17
  • 32
8
votes
2 answers

How to broadcast referral to android app intallation

Not Answered, Almost Same Question To Explain My Goal: I have an apk URL that could be in a market (eg: Google Play, Bazaar, etc) or could be a direct link to apk file. To Explain My Requirements I'll have to set referral link to broadcast it to…
Hossein Shahsahebi
  • 6,348
  • 5
  • 24
  • 38
8
votes
1 answer

Android reconnect to bluetooth device if connection lost

My phone is connecting to a bluetooth device and it works normally. It connect and the connection holds. I can change orientation, have my app in the background, I can close app and when I turn it back on it will automatically connect. But from app…
Tadej Vengust
  • 1,351
  • 4
  • 18
  • 35
8
votes
3 answers

Detect Lock Screen Incorrect Password by user in Android

I am building a security app and I need to know if the user is giving incorrect password . Suppose user phone is locked by pattern lock system , and unfortunately user has forgotten the pattern password.When user give wrong pattern 5 time, there…
8
votes
2 answers

Android keep BroadcastReceiver in background

I created a BroadcastReceiver and it runs only when my app shown in recent apps menu. If I remove my app from the recent apps the BroadcastReceiver will stop working. How can I keep the BroadcastReceiver in background? I register the…
8
votes
4 answers

Listen to location access disable/enable in settings

In Android OS, in "Settings" --> "Location services", there is a toggle button named "Access to my location" which can be used to disable & enable location info access from apps. Currently, I am developing a location service application. I am…
Mellon
  • 37,586
  • 78
  • 186
  • 264
8
votes
2 answers

In-Application BroadcastReceiver

Can anybody tell me a way to create an in-application BroadcastReceiver? I have created BroadcastReceiver which Toasts a message. It works even when the application is in background state, I want it to work only when application is in foreground. …
Rishabh Srivastava
  • 3,683
  • 2
  • 30
  • 58
8
votes
1 answer

Is there a way to tell if LocalBroadcastManager broadcasts were received?

Is there a way to tell if LocalBroadcastManager broadcasts were received? Or are being listened to? Basically I have an IntentService listening for Google Cloud Messages. When it gets one I need to either show a notification OR alert my main Service…
8
votes
1 answer

Long running service consumes a lot of battery

I developed an app and some people complains that it takes too much battery, it is the second most consuming process after the screen. However, in some devices it does not consumes that much battery. All the work my app do is in a service. The…
adl
  • 1,865
  • 1
  • 25
  • 32