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?
Related
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.
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.
I define BroadcastReceiver in an application manifest and the receiver receives events in onReceive as expected. However I do not create the receiver class so I can't pass any information about my activity which the receiver is supposed to control. Context parameter gives me only application context and no any activity reference. So what is right way to make communication between application activities and broadcast receiver?
The answer is a little twisted but I found it here as well
Inform Activity from a BroadcastReceiver ONLY if it is in the foreground
It seems working. Please close the question then.
You can communicate broadcast to activity from this:-
Intent intent=new Intent(context,YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
I have managed to get an Activity to start from my onReceive() methdod, but I really need to do a startActivityForResult();.
Is there any way I could do this?
On a side note, how would I make my app become a 'camera' app, as in it would appear when an app started the intent to take a picture?
The important thing to know about broadcast receivers is that you should not add long running processes in it, because after something like 5 seconds your app will crash.
The best thing to do in your case is to intent to other Activity from your broadcast receiver, and from that activity use startActivityForResult(), get the picture and continue from there...
startActivityForResult can only be called from an Activity since it is defined in the Activity class and require instance of activity.
You can only call startAcivity() from broadcast receiver since in onRecieve() you only have access to generic context object and it does not have startActivityForResult method defined in the class..
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.