Broadcast Receiver - Part 3, Listening to custom intent broadcasts

Sdílet
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

Komentáře • 66

  • @adityasarin16
    @adityasarin16 Před 7 lety +12

    I am becoming a fan of your videos.... to the point and with so much information

  • @amanaggarwal5884
    @amanaggarwal5884 Před 3 lety

    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.

  • @oumaimakabouri3491
    @oumaimakabouri3491 Před 5 lety

    helped in my preparation, Excellent explanations.thanks

  • @ronaldoavich
    @ronaldoavich Před 7 lety +3

    I eally like your succint explanations. Thanks. Keep up the quality.

  • @suyogdorlikar202
    @suyogdorlikar202 Před 7 lety +4

    hello sir,
    your video best video.
    it is helpful for study and also interview preparation.
    thanks

  • @akshayjain9794
    @akshayjain9794 Před 4 lety

    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 ....

  • @sunnysinha9728
    @sunnysinha9728 Před 5 lety +1

    helped in my project

  • @techitem9516
    @techitem9516 Před 6 lety

    I really very like your videos

    • @Codetutor-DemystifyCoding
      @Codetutor-DemystifyCoding  Před 6 lety

      Thanks.. Glad that you found them useful. Feel free to check out other content - visit playlist section. Hope you have subscribed.

  • @danielravi212
    @danielravi212 Před 4 lety

    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));

    }
    };

  • @seifkharrachi7322
    @seifkharrachi7322 Před 5 lety

    i like you when you say
    FOR THAT

  • @vijaynath1986
    @vijaynath1986 Před 5 lety

    Thank u gurudevvvvvvv

  • @vishalmishra1937
    @vishalmishra1937 Před 4 lety

    does second app have to be in foreground to get triggered by latter one

  • @mkc0321
    @mkc0321 Před 6 lety

    very good video

  • @kavie8257
    @kavie8257 Před 6 lety

    Hi,
    Thanks a lot for your tutorial. Could you please tell me the difference between "android:exported="false" " attribute and LocalBroadCasManager?

    • @Codetutor-DemystifyCoding
      @Codetutor-DemystifyCoding  Před 6 lety

      "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.

  • @gagansingh9642
    @gagansingh9642 Před 4 lety

    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.

  • @hs_harsh
    @hs_harsh Před 7 lety

    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!

    • @Codetutor-DemystifyCoding
      @Codetutor-DemystifyCoding  Před 7 lety +1

      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

  • @gaetano.the.crooner
    @gaetano.the.crooner Před 7 lety

    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

    • @Codetutor-DemystifyCoding
      @Codetutor-DemystifyCoding  Před 7 lety +1

      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

  • @safwenchaieb2915
    @safwenchaieb2915 Před 4 lety

    Thank you sir

  • @sajalhsn13
    @sajalhsn13 Před 5 lety

    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.

  • @52Tele
    @52Tele Před 5 lety

    Hello Sir,
    Is there a way to listen to any inter-app communication between two or more Android applications?

    • @Codetutor-DemystifyCoding
      @Codetutor-DemystifyCoding  Před 5 lety

      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.

    • @52Tele
      @52Tele Před 5 lety

      @@Codetutor-DemystifyCoding sure I will keep you posted.
      Thanks for your reply.

  • @oleksandrparnekov1371
    @oleksandrparnekov1371 Před 6 lety

    Hello! Dont work with LocalManager. What should I do? Thank you!

    • @Codetutor-DemystifyCoding
      @Codetutor-DemystifyCoding  Před 6 lety

      Did you download the code from Github link that I have provided in description section below?

  • @learn.build.develop.7467

    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?

    • @Codetutor-DemystifyCoding
      @Codetutor-DemystifyCoding  Před 7 lety

      I think you are getting the concept of BR wrong... Watch the complete series: czcams.com/play/PLfuE3hOAeWha9AWKnQTKP3YeZWfO1yamc.html

    • @Codetutor-DemystifyCoding
      @Codetutor-DemystifyCoding  Před 7 lety

      BR don't work on the concept of connectivity(Cellular, WiFi,Bluetooth etc)

  • @rameshvadapalliramesh5506

    can we send broadcast receiver from one activity to another activity in same app?

    • @Codetutor-DemystifyCoding
      @Codetutor-DemystifyCoding  Před 3 lety

      Yes... but why would u do it. You can just use implicit intents as well for that.

    • @rameshvadapalliramesh5506
      @rameshvadapalliramesh5506 Před 3 lety

      Codetutor yes...I’m trying to do but In another activity onReciceve() is not calling..I used localbroadcastmanager

  • @rahulnag9582
    @rahulnag9582 Před 5 lety

    Hi sir,
    can you please tell some real time example of using localBrodcastManager
    or some existing app example which are using this.
    Thanks,
    Rahul

    • @Codetutor-DemystifyCoding
      @Codetutor-DemystifyCoding  Před 5 lety +1

      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.

    • @rahulnag9582
      @rahulnag9582 Před 5 lety

      @@Codetutor-DemystifyCoding thank you sir.. 😊

    • @rahulnag9582
      @rahulnag9582 Před 5 lety

      Sir,
      I have sent u request on LinkedIn.
      please accept it ....🙏🙏

    • @Codetutor-DemystifyCoding
      @Codetutor-DemystifyCoding  Před 5 lety

      Hahaha.. Done!!! I think...

  • @abhisheksengupta4159
    @abhisheksengupta4159 Před 6 lety

    Hello Sir, Simple custom broadcast receiver is not working ,what can go wrong ?

    • @Codetutor-DemystifyCoding
      @Codetutor-DemystifyCoding  Před 6 lety

      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?

  • @techitem9516
    @techitem9516 Před 6 lety

    But please put video custom broadcast receiver in oreo because it will not work in oreo.
    All your android tutorial give me deep knowledge

  • @pankajtolwani6436
    @pankajtolwani6436 Před 5 lety

    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?????

    • @pankajtolwani6436
      @pankajtolwani6436 Před 5 lety

      this is from your first part of this video..

    • @Codetutor-DemystifyCoding
      @Codetutor-DemystifyCoding  Před 5 lety

      Yeah.. This is specific to Oreo. Please have a look at latest video I published on this topic - czcams.com/video/1r2hSODTMHM/video.html

  • @Bhuvanendrapandian
    @Bhuvanendrapandian Před 3 lety

    Will broadcast receiver work when we kill the app?

  • @dipshikasharma8848
    @dipshikasharma8848 Před 7 lety

    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.

    • @Codetutor-DemystifyCoding
      @Codetutor-DemystifyCoding  Před 7 lety

      +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..

  • @techitem9516
    @techitem9516 Před 6 lety

    Please sir I have error in custom broadcast receiver in oreo.
    Not work.
    So please give me suggestions

    • @Codetutor-DemystifyCoding
      @Codetutor-DemystifyCoding  Před 6 lety

      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.

    • @techitem9516
      @techitem9516 Před 6 lety

      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

    • @techitem9516
      @techitem9516 Před 6 lety

      Sir it's working
      Thanks a lot sir
      Please give me your contact so in future I can solve more bug from you. Sir.

  • @AnemUday
    @AnemUday Před 6 lety

    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.

    • @Codetutor-DemystifyCoding
      @Codetutor-DemystifyCoding  Před 6 lety

      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.

  • @lakshmikantdeshpande2347

    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)

    • @lakshmikantdeshpande2347
      @lakshmikantdeshpande2347 Před 6 lety +1

      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);

    • @Codetutor-DemystifyCoding
      @Codetutor-DemystifyCoding  Před 6 lety

      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!!!

    • @lakshmikantdeshpande2347
      @lakshmikantdeshpande2347 Před 6 lety +1

      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 ;)

    • @sahildhar454
      @sahildhar454 Před 6 lety

      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.