kotlin broadcast receiver example
In addition to providing a mechanism for launching application activities, intents are also used as a way to broadcast system-wide messages to other components of the system. This involves the implementation of Broadcast Intents and Broadcast Receivers, both of which are the topic of this chapter.
Broadcast intents are Intent objects that are broadcast via a call to the send broadcast(), send a sticky broadcast() , or sendOrderedBroadcast() methods of the Activity class (the latter being used when results are required from the broadcast). In addition to providing a messaging and event system between application components, broadcast intents are also used by the Android system to notify interested applications about key system events (such as the external power supply or headphones being connected or disconnected).
1.MainActivity.kt:
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
// register the receiver in the main activity in order
// to receive updates of broadcasts events if they occur
lateinit var receiver: AirplaneModeChangeReceiver
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
receiver = AirplaneModeChangeReceiver()
// Intent Filter is useful to determine which apps wants to receive
// which intents,since here we want to respond to change of
// airplane mode
IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED).also {
// registering the receiver
// it parameter which is passed in registerReceiver() function
// is the intent filter that we have just created
registerReceiver(receiver, it)
}
}
// since AirplaneModeChangeReceiver class holds a instance of Context
// and that context is actually the activity context in which
// the receiver has been created
override fun onStop() {
super.onStop()
unregisterReceiver(receiver)
}
}
2.activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</androidx.constraintlayout.widget.ConstraintLayout>
3.AirplaneModeChangeReceiver.kt:
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.widget.Toast
// AirplaneModeChangeReceiver class extending BroadcastReceiver class
class AirplaneModeChangeReceiver : BroadcastReceiver() {
// this function will be executed when the user changes his
// airplane mode
override fun onReceive(context: Context?, intent: Intent?) {
// intent contains the information about the broadcast
// in our case broadcast is change of airplane mode
// if getBooleanExtra contains null value,it will directly return back
val isAirplaneModeEnabled = intent?.getBooleanExtra("state", false) ?: return
// checking whether airplane mode is enabled or not
if (isAirplaneModeEnabled) {
// showing the toast message if airplane mode is enabled
Toast.makeText(context, "Airplane Mode Enabled", Toast.LENGTH_LONG).show()
} else {
// showing the toast message if airplane mode is disabled
Toast.makeText(context, "Airplane Mode Disabled", Toast.LENGTH_LONG).show()
}
}
}
No comments:
Post a Comment