Should BroadcastReceiver be declared inside activities? - android

I'm wondering what's the better cleaner design approach to handle BroadcastReceiver creation between those two:
Declaring directly a BroadcastReceiver in an Activity/Fragment and overriding onReceived method there.
Creating another custom receiver class in a package "receiver", that extends the BroadcastReceiver class and override onReceive. Then, you instantiate that custom receiver class in your Activity/Fragment.

There are 2 types of Broadcast Receivers
1)Dynamic Broadcast Receiver that is registered inside activity
2)Static Broadcast Receiver that is registered or declared inside Manifest file with Receiver tag with specific action(when to fire the Receiver)
Uses:Eg say receive message
1) Dynamic Broadcast Receiver is used to receive the Broadcast intent mainly to refresh the message sent through send Broadcast method.
2)Static Broadcast Receiver is used to receive the message.Here the Registered Receiver with SMS receive action will receive the message and later it sent to activity
At last It depends on developer which one to use,when to use based on requirement

There is no cleaner approach, it depends on how you Code.
Following the DRY principle, I only extend a BroadcastReceiver as soon as I notice I'll need them in different Activites.

Related

How to avoid broadcast from being wasted when receiver is registered inside activity?

I have a service which will broadcast a message to broadcast receiver which is registered in some activity. The activity might be running or not. If it's open then broadcast will be received. But if not, the broadcast will be wasted. Is there a way to avoid this broadcast from being wasted without adding the broadcast inside manifest?
Consider to start/stop that service based on Activity's lifecycle, when Activity is created (onCreate()) start that service. When Activity is destroyed (onDestroy()) stop the service so you avoid "waste" those messages and even have a service running.
1- Create an inner class in your activity that extends BroadcastReceiver.
2- Create an object of your receiver and an intent filter.
3- Override onResume and registerReceiver(receiver, intentFilter);
4- Override onPause and unregisterReceiver(receiver);
Take a look at this answer. you dont have to add it in the manifest.
answer here

BroadcastReceiver notification and activity messages

I'm very new to Android and I need a little assistance here.
I have an activity and a broadcast receiver.
Broadcast receiver should listen SMS Intents and:
if Activity is visible and interacting - SMS should appear in activity
if no - broadcast receiver should create notification with SMS, so if user click on that notification, the activity will be shown
So, i read a lot of articles about broadcasts and i can implement one of these variant(1 - as a inner broadcastreceiver class and 2 - as global broadcast receiver declared in manifest) , but how i can have both? Could you please propose some idea?
Ok, so in that case i post my own solution.
I declare two broadcast receiver: one, declared in Manifest, and doing post to tray notification, and second - declared as an inner class of main activity and doing post to activity components. Also i have subclass of Application class to track main activity state (i.e. visible or not). And so global broadcast receiver post any notifications only if it see that activity is hide right now.
I think its reasonably solution.

boot_completed action to register programmatically and not in manifest

I was trying to register a receiver programmatically for actionandroid.intent.action.BOOT_COMPLETED ,
i.e
Lets take i have reciever class named BootReceiver which extends BroadCastReceiver class.
So in one of my activity class i have written this code,
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.intent.action.BOOT_COMPLETED");
intentFilter.addAction("android.intent.action.PACKAGE_REPLACED");
BootReceiver receiver = new BootReceiver();
getApplicationContext().registerReceiver(receiver, intentFilter);
I was trying to do both update of app as well boot_completed action to same broadcast receiver.
So what i tried is,
I ran the activity with registering of above code and then restarted the device. I was not getting any callback to BootReceiver onReceive() method.
Is it possible to programmatically declare receiver for the boot_completed action or is it necessary to declare receiver in manifest file.
Actually my requirement is to programmatically declare it.
Thanks in advance.
I was trying to register a receiver programmatically for actionandroid.intent.action.BOOT_COMPLETED
By the time registerReceiver() is called, the boot will have long since happened. The only place to register for android.intent.action.BOOT_COMPLETED is in the manifest, as that is able to register interest in broadcasts even when you do not have a process running.
Is it possible to programmatically declare receiver for the boot_completed action
No. You can request it, but it will never work.
is it necessary to declare receiver in manifest file
If you want one to work, yes.

android intent startActivity() and sendBroadcast()

What are the differences between startActivity() and sendBroadcast()?
We can use startActivity() to start a activity.sendBroadcast() can do this too.what are the differences?
They do look similar, cause they have same function: send a message to receiver.
You can tell the difference from the receiver.
startActivity() can only has one receiver, in other words, only one receiver can receiver it's message(intent).
But for sendBroadcast(), multiple receivers can receive the message.
Here we go,
startActivity() - gets an intent and open its attached activity.
You have two ways how to use it - explicit - by adding activity name to the intent or implicit by adding only action(Ex. Intent.ACTION_SENDTO).
activity.sendBroadcast() - broadcast is a totally different thing than intent.
With broadcasts you can communicate with any registered broadcast receiver, in practice - sending messages to different component within/outside your application.
sendBroadCast() sends a global broadcast that is to be picked up by any BroadcastReceivers that are set to receive that broadcast.
startActivity() attempts to start an Activity based on either the class name you specify or the Intent Action (which is a String).
REF : sendBroadscast VS startActivity. What is the difference?

How BroadcastReceiver is different from Intent

Hello I see contradicting definitions. Android experts, can you explain this to me please?
1) If BroadCastReceiver is a component in android that responds to intents, then I can as well register an filter for activity in androidManifest xml file and have it do my job based on intent like battery low, no network, orientation change etc. these are intents I might be interested to react in my code.
2) Why register whole another filter for BroadcastReceiver in androoidManifest.xml and perform action at onReceive() inside the BCRCVR class?
3.Can we really perform intent driven operations in an activity? yes right?
I guess the title should be "How BroadcastReceiver is different from Activity".
In my opinion, Broadcast receiver is independent unit, because sometimes you don't want the system to create a new Activity object just to handle arrived intent. Moreover, you don't your activity to be shown. Broadcast receivers are independent and can be used outside any Activity. Activity is more about user interface and broadcast is about handling events.

Categories

Resources