Broadcast Receiver - Part 3, Listening to custom intent broadcasts
Vložit
- čas přidán 27. 09. 2016
- In this video we discuss how to broadcast an intent so that a Broadcast Receiver can listen to it and respond to it. The video also demonstrates the use of exported attribute to secure the broadcast Receiver and also how to use LocalBroadcastManager to restrict the intent broadcast propagation outside the app.
Follow me on Twitter: / anilvdeshpande
Source Code GitHub links:
SampleBRTutorial: github.com/AnilDeshpande/Samp...
LocalBRTester: github.com/AnilDeshpande/Loca... - Věda a technologie
I am becoming a fan of your videos.... to the point and with so much information
kamal ho sir kha chup k baithe the hum angrezo ki video dekhte reh gye aur app yha bdiya content de rhe ho woh dekha nhi tha aaj tak.
I eally like your succint explanations. Thanks. Keep up the quality.
+Ronald Mann thanks for the kind words...
helped in my preparation, Excellent explanations.thanks
hello sir,
your video best video.
it is helpful for study and also interview preparation.
thanks
You are welcome... stay tuned....
helped in my project
I really very like your videos
Thanks.. Glad that you found them useful. Feel free to check out other content - visit playlist section. Hope you have subscribed.
Thank u gurudevvvvvvv
i like you when you say
FOR THAT
first all your videos are awesome , i have a question that how and where did you mentioned about the second app in your first app that it will receive the msg , like when i send a msg to someone i have to mention or write a phone number (like an address to the other app user )... where did you do that in your app that it could know that which app will receiver the msg ... thank you ..
and again your videos are awesome ....
very good video
Thanks.. Glad that you found the content useful.
Hi,
Thanks a lot for your tutorial. Could you please tell me the difference between "android:exported="false" " attribute and LocalBroadCasManager?
"android:exported="false" is used to prevent the broadcast receiver (BR) from responding to external intent broadcast.
LocalBroadcastReceiver is used to broadcast intent within the app. In a way they complement one another. If you want to have a BR that is local to an application, I would rather suggest you use both.
Sir, this is working but when I remove Receiver app from "recent apps" and fire broadcast from another app then that receiver app doesn't recieve that broadcast, however i have set exported attribute to true and declared receiver both in manifest and java file...please help.
Great video Sir! I am using Broadcastreceiver in a college project of mine and got stuck. Hopefully you can help.
The project is a bluetooth project. The bluetooth part is working perfectly. I am receiving the data in this format "3.6666&8.9999". These are latitude and longitude values I need to plot in a map. But I am unable to use the values outside the Broadcastreceiver. Please do help. I have attached the code below.
BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String text = intent.getStringExtra("theMessage");
// The "3.6666&8.9999" is inside this
Log.i("text sample", text);
String coordinates[] = text.split("&");
btlatitude = Double.parseDouble(coordinates[1]);
//3.6666
btlongitude = Double.parseDouble(coordinates[0]);
8.9999
incomingMessages.setText(text);
Log.i("text sample lat", String.valueOf(btlatitude));
Log.i("text sample long", String.valueOf(btlongitude));
}
};
does second app have to be in foreground to get triggered by latter one
i really a great vedio!
sir i want to ask is there requires any permission to show the BR toast in other app!
since i tried i was perfect for SampleBRTutorial but didnt toasted for localBRtester!
note: no error msz or sort of thing
suggestion are heartly accepted!
I am not sure whether I understood the question correctly. But met me try
sir i want to ask is there requires any permission to show the BR toast in other app! - You want to know whether you need any permission to show BR in another app: No permissions are needed but you have to make sure that the you use correct intent filters and BR in another App needs to use this filter correctly.
I suggest you understand the concept you implicit intent and then go through the complete BR series on my channel - I have got it covered
So interesting sir.
Here's my problem:
I've already implemented a Firebase Cloud Messaging and all works fine only when my app is in foreground.
But when my app is in background, notifications are delivered to system tray and not to Firebase Service (so I only got Default color and Default Icon instead of Vibration, Led Color etc).
In order to receive and manage incoming notifications from firebase console what do i have to implement?
Service or Broadcast Receiver? And how is the implementations?
Thank you in advance
Gaetano
To answer this, it would definitely require intimate knowledge of Firebase framework. Unfortunately I have not worked on this framework so far. I would suggest that you look out on other developer forums like stackoverflow, blogs and even official firebase tutorials and API documentation
Hello Sir,
Is there a way to listen to any inter-app communication between two or more Android applications?
If apps are designed securely.. there is not way of hacking your way in to two app's (processes) communication - which typically happens through Intents, Messenger API or AIDL. That is why it is very important to be careful while creating Services and Broadcast Receivers in Android - not to expose them by accident. So to answer your question, at least I am not aware of any way Android framework provides a mechanism to intercept communication between two apps in Android device. Let me know in case you find out anything interesting.
@@Codetutor-DemystifyCoding sure I will keep you posted.
Thanks for your reply.
Hello! Dont work with LocalManager. What should I do? Thank you!
Did you download the code from Github link that I have provided in description section below?
Thank you sir
This might help you. stackoverflow.com/questions/20441308/boot-completed-not-working-android
Sir can i know the range of the Broadcast receiver? What if i am alot of miles away and will send the broadcast, will it still work?
I think you are getting the concept of BR wrong... Watch the complete series: czcams.com/play/PLfuE3hOAeWha9AWKnQTKP3YeZWfO1yamc.html
BR don't work on the concept of connectivity(Cellular, WiFi,Bluetooth etc)
hi sir,
I have used your code but not getting any message when i press button(broadcast is sent but not received) .. what can be the possible mistake im doing?????
this is from your first part of this video..
Yeah.. This is specific to Oreo. Please have a look at latest video I published on this topic - czcams.com/video/1r2hSODTMHM/video.html
Hello Sir, Simple custom broadcast receiver is not working ,what can go wrong ?
Either it might have been not registered properly or Intent might be wrong. Did you debug the application? What error are you getting in LogCat?
I am using android PIE. I saw the quick fix update. Now I can trigger BR locally but having problem with remote access. BR is not responding while I want to access from remote app.
I should work. Did you try to debug the application?
can we send broadcast receiver from one activity to another activity in same app?
Yes... but why would u do it. You can just use implicit intents as well for that.
Codetutor yes...I’m trying to do but In another activity onReciceve() is not calling..I used localbroadcastmanager
nice. but I want to add like when I sent number and then I got otp like when we login on WhatsApp it show us otp how I add this function in Android studio project .please help and reply sooner.
+Bhaskar soni this can be accomplished through a straight forward observer design pattern. Basically the Activity which needs to display the OTP will have to register as an observer. Thus when BR is triggered it just notifies all the registered observers. But keeping track of registered observers should not be done inside the BR rather in some other place as BR is stateless. So it can get little bit tricky. But I am sure you will get enough code samples on Stackoverflow... Best of luck...Happy coding..
But please put video custom broadcast receiver in oreo because it will not work in oreo.
All your android tutorial give me deep knowledge
Will broadcast receiver work when we kill the app?
Yes...
Hi sir,
can you please tell some real time example of using localBrodcastManager
or some existing app example which are using this.
Thanks,
Rahul
This is used when you don't want the intent to propagate outside of the app. Let's say you want to trigger a notification when you start a Service through Broadcast Receiver. You don't want this intent to be propagated outside the application. In case you have want to broadcast an intent with actions - which you are not sure may be defined in any other app. As a precaution you once again use local broadcast manager. Hope I am making sense.
@@Codetutor-DemystifyCoding thank you sir.. 😊
Sir,
I have sent u request on LinkedIn.
please accept it ....🙏🙏
Hahaha.. Done!!! I think...
Please sir I have error in custom broadcast receiver in oreo.
Not work.
So please give me suggestions
Looks like since Oreo, Android has made it mandatory to to register Broadcast Receiver using context.registerReceiver method. Just the declaration of Broadcast Receiver in the Manifest file will not suffice. I suggest you download the demo code (link available in description section below the video) and modify to see it work.
Thanks sir
And please give me contact email or what's up so I can take help easily from you.
I already subscribed and share your videos . Because it is really very helpful
Thanks a lot for helpful reply
Sir it's working
Thanks a lot sir
Please give me your contact so in future I can solve more bug from you. Sir.
Your video content is very nice, except it is too fast. Beginners will take little time to understand, but as it is fast they miss some points. They may need to refer same video couple of times.
Yeah I can understand. In some of these older videos I have spoken with natural speed - which is quite fast. I am still trying to learn to curtail my natural speaking instinct which is quite fast. I am not a professional voice over artist. I believe some of my recent videos this is much better in terms of speed of the speech. I hope keep improving on this. Thanks for the feedback.
Hi,
I tried using LocalBroadcastManager in LocalBRTester.
It didn't send the broadcast out of the app. That is good.
I tried using LocalBroadcastManager in SampleBRTutorialCode.
It should send the broadcast, right ?
I'm not getting the Toast message.
Here's a snippet from my Manifest:
I tried making android:exported as "true" as well. It didn't help. What could be the issue ?
(action, category and everything else is correct. That's definitely not a problem)
I fixed it by registering it with LocalBroadcastManager.
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
intentFilter.addAction("a.b.c.d");
lbm.registerReceiver(broadcastReceiver, intentFilter);
Then for sending the broadcasts:
lbm.sendBroadcast(intent);
The main reason why this works is last line."lbm.sendBroadcast(intent)". For triggering app specific broadcasts you have to use the instance of LocalBroadcastManager. If you use this[application context] it will be triggering global intent and LocalBroadcastManager will will respond to it. Most of people miss this. Great going!!!
What i meant to say is, for receiving broadcasts sent by LocalBroadcastManager, we have to register with LocalBroadcastManager instance.
i.e. LocalBroadcastManager lbm = new LocalBroadcastManager();
lbm.registerReceiver(broadcastReceiver, intentFilter)
Thanks bdw :)
This tutorial series is easier to understand than Google's official course ;)
thanks this helps a lot, I would like to highlight one thing that I observed while practicing LocalBroadcastManager code, if you have a broadcast receiver in a separate file or its a separate component with exported property set to true, then any other app can send broadcast intents to it even if you have registered that broadcast receiver with LocalBroadcastManager instance, however if you have created a new instance of broadcast receiver with in a same file which means there will be no additional components for that broadcast receiver in manifest file no other app can send data to that receiver. In video, the author is restricting the sender app LocalBRTester to send intents to MyBroadcastReceiver from SampleBRTutorialCode package, but any other app can send intents to MyBroadcastReceiver. To my understanding, the main advantage of using LocalBroadcastManager is to dynamically generate Broadcastreceiver to fulfill event based functionality.