Search code examples
androidandroid-activitybroadcast

Is it possible to receive a result of a broadcast in the activity that launches the broadcast?


I'm developing an application in Android Studio. This application has a fragment, say A, that launches an activity, say B, when user clicks. The activity B launches a broadcast, there reads a received SMS and gets a code. This code has to be sent to the activity B, and the activity B has to write the code in an editText.

So, the application receives a code verification for login... how can I do it?

Imagen 1

Imagen 2


Solution

  • It's possible, but not with sendBroadcast(). Instead, you'll have to use sendOrderedBroadcast().

    However, this does require you to setup a receiving BroadcastReceiver, since by default, only BroadcastReceivers can listen to a Broadcast, so even if an Activity is allowed to send a Broadcast, it can't listen to one, even if it's feedback.

    But if you don't want to use sendOrderedBroadcast(), there's nothing stopping you from having your BroadcastReceiver send a result Broadcast to a BroadcastReceiver that you've setup in your Activity.

    Yes, both solutions sounds extremely similar, because the requirements to allow an Activity to be able to listen to a broadcast is the same.