Broadcast receiver throughout the Application - android

I have an application which tells the state of the device (Connected/Disconnected). Now I have registered the Broadcast Receiver for the same and display the appropriate state.
1. But my problem is that I have to make this status message common to all the activities. So where do I register this receiver and where do I unregister the receiver. I dont want the repeatative code. So is there any place from which I can do an common register and unregister instead of unregistering it in every activity for onDestroy().
Can I use the Application class in any way so as to have an common place to register and unregister?
Or is the Manifest file appropriate in my case to register the receiver
Thanks,
Shraddha

Android provides BroadcastReceivers, which can be independent of an Activity. These can be "registered" in your applications manifest. Of course, you need a corresponding programmed receiver to which the manifest corresponds when the suitable intent is called.
See here: http://developer.android.com/guide/topics/manifest/intent-filter-element.html

Related

When does Broadcast Receiver start?

I'm new to android and trying to build a simple app which needs to listen for incoming sms. I know that I need to use the BroadcastReceiver class and I also know how to make my own broadcast receiver. But how do I start it? Does it start automatically if I set the code for it in the manifest? The app just has the Main activity, do I need to somehow add a broadcast receiver in the onCreate of this activity? I searched for an answer, but it's still not clear to me. I know it's not nice to ask, but it would be great if you can share some sample code. Thanks!
If you declare the receiver within your AndroidManifest.xml, then you shouldn't need to do anything more. When a broadcast gets sent, the Android system will look through all installed apps and notify each app that has declared the appropriate Receiver in its manifest, starting the app in the process if necessary. For most cases, such as SMS, that is how you want to declare receivers, because most broadcasts are sent with the intent that you want to open your app when its not currently running to react to the broadcast.
Alternatively, you may declare the broadcast within a running activity, which may be useful if you want the broadcast to directly update the UI in your running app.
BroadcastReceiver Documentation

Difference between registering intent through Android Manifest file and programmatic registering in android

I understand the basic difference between the two types of intent registering.
But i want to know is there any difference in terms of speed??
Registering broadcast from code and through manifest doesn't have much difference except these points mentioned in the documentation:
1)When you use registerReceiver(BroadcastReceiver, IntentFilter), any application may send broadcasts to that registered receiver. You can control who can send broadcasts to it through permissions described below.
2)When you publish a receiver in your application's manifest and specify intent-filters for it, any other application can send broadcasts to it regardless of the filters you specify. To prevent others from sending to it, make it unavailable to them with android:exported="false".
Read more in developer documentations: http://developer.android.com/reference/android/content/BroadcastReceiver.html
You can use them based on your requirement.
The BroadcastReceiver class (when launched as a component through a manifest's tag) is an important part of an application's overall lifecycle.
If you registerRecevier in onResume method in activity and unregisterReceiver in onPause() method. Your receiver life time is this activity life time.

Until when does a BroadcastReceiver receive intents?

I am a bit confused about this part of documentations:
A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent).
Once your code returns from this function, the system considers the object to be finished and no longer active.
What does "no longer active" mean? It means our receiver won't receive any events with the specified type? Or it means the receiver is destroyed and a new one is created when the event occurs?
Is this any difference about this, between registering receiver in manifest file and in code dynamically?
"Is this any difference about this, between registering receiver in manifest file and in code dynamically?"
Yes, there is a difference. Receivers registered in the manifest file will receive all intent-filters it matches, anytime the OS sends them out. This differs from registering a broadcast receiver in code by the broadcast receiver in code will only listen to broadcast intents while you have set it to listen. The reason you would use this method sometimes, as opposed to just registering it in the manifest, is if you wanted to implement ordered broadcast. For example, (you can build your application in such a way that) a broadcast receiver in an activity would have higher priority than the manifest, that way, if you receive an intent that your application handles, you can present a message in the activity because you know that the user much currently be in your app. If the broadcast receiver is not listening in the activity, then you assume the user is not currently using your app so then you may just want to send a notification. I should mention that ordered broadcast have the ability to abort the propagation of a broadcast intent to the next receiver, which is what you would do if you caught the intent in your activity class ,therefore, the manifest file will only get the intent if the receiver in the activity class did not catch it.
The words "no longer active" mean that the broadcast receiver will just stop doing any work for that particular broadcast. It will still listen to any succeeding broadcast intents just fine.
It means that upon returning from onReceive() the system will consider its process to be empty and aggressively kill it so that resources are available for other more important processes.
You will often use a Service in conjunction with a BroadcastReceiver to keep the containing process active for the entire time of your operation.

understanding intent-filter in android manifest.xml

In reading some android documentation, it seems like intent-filter indicates what intents the application is interested in. In my application, I am interested in receiving BluetoothAdapter.ACTION_STATE_CHANGED. I didn't declare it in my manfiest xml file, but in my application I register a BroadcastReceiver that filters on BluetoothAdapter.ACTION_STATE_CHANGED and I receive the events just fine.
Can someone explain then why I would use intent-filter?
So your application can receive the event even if it's not already running.
See http://developer.android.com/guide/topics/manifest/receiver-element.html:
Declares a broadcast receiver (a BroadcastReceiver subclass) as one of the application's components. Broadcast receivers enable applications to receive intents that are broadcast by the system or by other applications, even when other components of the application are not running.
There are two ways to make a broadcast receiver known to the system: One is declare it in the manifest file with this element. The other is to create the receiver dynamically in code and register it with the Context.registerReceiver() method. See the BroadcastReceiver class description for more on dynamically created receivers.

Can someone give me some scenarios why we need to register BroadcastReceiver dynamically?

As title, I am wondering under what situation we must register the BroadcastReceiver dynamically in an Activity? Can somebody give me some scenarios the statically registered receivers cannot fulfill?
Thanks:)
Can somebody give me some scenarios the statically registered receivers cannot fulfill?
In addition to #Pedro Loureiro's scenario, certain system broadcasts (e.g., ACTION_BATTERY_CHANGED, ACTION_SCREEN_OFF, ACTION_SCREEN_ON) can only be received by dynamically-registered broadcast receivers.
Also, there will be patterns where you will register dynamically for a broadcast to be sent from your own service, such as the activity-or-Notification scenario I outline in this blog post.
For example, you may have an application and in the preferences there might be an option to notify the user about certain events. When this option is on, you would register the corresponding broadcast receiver.

Categories

Resources