安卓开发_广播机制_发送自定义广播
安卓开发_广播机制_发送自定义广播
- 发送标准广播
- 发送有序广播
- 参考
发送标准广播
新建一个MyBroadcastReceiver用于接收广播, 代码如下:
class MyBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Toast.makeText(context, "received in MyBroadcastReceiver", Toast.LENGTH_SHORT).show()
}
}
再AndroidManifest.xml中修改MyBroadcastReceiver, 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
...
<application
...>
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.broadcasttest.MY_BROADCAST" />
</intent-filter>
</receiver>
...
</application>
</manifest>
定义一个用于发送广播的按钮, 代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send Broadcast"
/>
</LinearLayout>
给按钮加入发送自定义广播的逻辑, 代码如下:
class MyActivity : AppCompatActivity() {
private lateinit var binding: ActivityMyBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMyBinding.inflate(layoutInflater)
setContentView(binding.root)
// 发送标准广播
binding.button1.setOnClickListener {
val intent = Intent("com.example.broadcasttest.MY_BROADCAST")
// 指定要发送给哪个程序后, 才会变成显示广播, 静态注册的Broadcast才可以接收到这条广播
intent.setPackage(packageName)
sendBroadcast(intent)
}
}
}
发送有序广播
再创建一个新的BroadcastReceiver, 代码如下:
class AnotherBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Toast.makeText(context, "received in AnotherBroadcastReceiver", Toast.LENGTH_SHORT).show()
}
}
在AndroidManifest.xml中修改AnotherBroadcastReceiver,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
...
<application
...>
<receiver
android:name=".AnotherBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.broadcasttest.MY_BROADCAST" />
</intent-filter>
</receiver>
...
</application>
</manifest>
此时点击"Send Broadcast", 会分别弹出俩次提示信息.
接下来再定义一个按钮并添加逻辑, 代码如下:
class MyActivity : AppCompatActivity() {
private lateinit var binding: ActivityMyBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMyBinding.inflate(layoutInflater)
setContentView(binding.root)
// 发送有序广播
binding.button2.setOnClickListener{
val intent = Intent("com.example.broadcasttest.MY_BROADCAST")
// 第二个参数是一个与权限相关的字符串
sendOrderedBroadcast(intent, null)
}
}
}
设定BroadcastReceiver的先后顺序, 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
...>
<receiver
...>
<intent-filter android:priority="100">
...
</intent-filter>
</receiver>
</application>
</manifest>
修改MyBroadcastReceiver中的代码, 代码如下所示:
class MyBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Toast.makeText(context, "received in MyBroadcastReceiver", Toast.LENGTH_SHORT).show()
// 截断广播
abortBroadcast()
}
}
参考
郭霖. 《第一行代码 Android 第3版》