android中的广播是系统范围的事件,当设备启动,在设备上接收到消息或接收到来电,或者设备进入飞行模式等情况下可能发生。广播接收器用于响应这些全系统事件。广播接收器允许我们注册系统和应用程序事件,并且当该事件发生时,注册接收器会收到通知。主要有两种类型的广播接收器:
- 静态广播接收器:这些类型的接收器在清单文件中声明,即使关闭了应用程序也可以使用。
- 动态广播接收器:仅当应用程序处于活动状态或最小化时,这些类型的接收器才能工作。
从API级别26开始,大多数广播只能由动态接收器捕获,因此我们在下面给出的示例项目中实现了动态接收器。在Intent类中定义了一些静态字段,可用于广播不同的事件。我们已经将飞行模式的更改作为广播事件,但是有许多事件可以使用广播寄存器。以下是一些在系统范围内产生的重要意图:
Intent |
Description Of Event |
---|---|
android.intent.action.BATTERY_LOW : | Indicates low battery condition on the device. |
android.intent.action.BOOT_COMPLETED | This is broadcast once after the system has finished booting |
android.intent.action.CALL | To perform a call to someone specified by the data |
android.intent.action.DATE_CHANGED | Indicates that the date has changed |
android.intent.action.REBOOT | Indicates that the device has been a reboot |
android.net.conn.CONNECTIVITY_CHANGE | The mobile network or wifi connection is changed(or reset) |
android.intent.ACTION_AIRPLANE_MODE_CHANGED | This indicates that airplane mode has been switched on or off. |
为了在我们的应用程序中使用广播接收器,我们必须做的两件事是:
- 创建广播接收器:
class AirplaneModeChangeReceiver:BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
// logic of the code needs to be written here
}
}
- 注册BroadcastReceiver:
IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED).also {
// receiver is the broadcast receiver that we have registered
// and it is the intent filter that we have created
registerReceiver(receiver,it)
}
例子
下面是一个示例项目,显示了如何创建广播接收器以及如何为特定事件注册它们以及如何在应用程序中使用它们。
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Kotlin作为编程语言。
步骤2:使用activity_main.xml文件
转到activity_main.xml文件,并参考以下代码。以下是activity_main.xml文件的代码。
XML
Kotlin
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)
}
}
Kotlin
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 excecuted 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()
}
}
}
步骤3:使用MainActivity.kt文件
转到MainActivity.kt文件,并参考以下代码。下面是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)
}
}
步骤4:建立新的Kotlin类别
转到应用程序> Java >您的包名称(其中存在MainActicity)>右键单击>新建> Kotlin File / Class并将文件命名为AirplaneModeChangeReceiver 。以下是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 excecuted 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()
}
}
}