Created
March 22, 2024 10:31
-
-
Save talhahasanzia/cd1d4de42b5141c00039da847653623d to your computer and use it in GitHub Desktop.
Ordered Broadcast in Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Send ordered broadcast | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
// Create the intent for the ordered broadcast | |
val orderedBroadcastIntent = Intent("com.example.ORDERED_BROADCAST") | |
orderedBroadcastIntent.putExtra("message", "Hello from MainActivity!") | |
// Send the ordered broadcast | |
sendOrderedBroadcast(orderedBroadcastIntent, null) | |
} | |
} | |
// abort broadcast | |
class MyOrderedBroadcastReceiver : BroadcastReceiver() { | |
override fun onReceive(context: Context, intent: Intent) { | |
// Receive the broadcast | |
val action = intent.action | |
if ("com.example.ORDERED_BROADCAST" == action) { | |
// Log that the receiver has received the broadcast | |
val message = intent.getStringExtra("message") | |
println("Ordered Receiver Received: $message") | |
// Do some processing or action here | |
// Example of aborting the broadcast | |
abortBroadcast() // This will prevent further receivers from receiving the broadcast | |
// Log that the broadcast has been aborted | |
println("Ordered Receiver Aborted Broadcast") | |
} | |
} | |
} | |
// Sending an Ordered Broadcast with initial data | |
val initialMessage = "Hello from the sender!" | |
val intent = Intent("com.example.ORDERED_BROADCAST").apply { | |
putExtra("message", initialMessage) | |
} | |
sendOrderedBroadcast(intent, null) | |
// Manipulating data | |
class OrderedBroadcastReceiver : BroadcastReceiver() { | |
override fun onReceive(context: Context, intent: Intent) { | |
val action = intent.action | |
if ("com.example.ORDERED_BROADCAST" == action) { | |
// Retrieve the initial message from the broadcast | |
val initialMessage = intent.getStringExtra("message") | |
// Log.d initialMessage : Hello from the sender! | |
println("initialMessage: $initialMessage") | |
// Modify the message | |
val modifiedMessage = initialMessage?.toUpperCase() + "-Modified" // Example modification | |
// Pass the modified message to the next receiver | |
setResultData(modifiedMessage) | |
} | |
} | |
} | |
// Getting updated data and aborting broadcast | |
class OrderedBroadcastReceiver : BroadcastReceiver() { | |
override fun onReceive(context: Context, intent: Intent) { | |
val action = intent.action | |
if ("com.example.ORDERED_BROADCAST" == action) { | |
// Retrieve the modified message from the broadcast | |
val modifiedMessage = intent.getStringExtra("message") | |
// Log.d modifiedMessage : HELLO FROM THE SENDER!-Modified | |
println("modifiedMessage: $modifiedMessage") | |
// This receiver can also abort further deliveries | |
abortBroadcast() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment