網(wǎng)站首頁 編程語言 正文
一、動態(tài)注冊和靜態(tài)注冊
動態(tài)注冊:在代碼中注冊
動態(tài)注冊能監(jiān)聽到大部分的系統(tǒng)廣播和自定義的廣播(都為隱式廣播)
步驟
1.新建一個Broadcast Receiver的子類,重寫onReceiver方法,當(dāng)接收到廣播時,會執(zhí)行onReceiver方法里面的內(nèi)容。
2.調(diào)用registerRecevier方法注冊,將intentFilter和Broadcast Receiver的子類的實例代入,其中intentFilter調(diào)用addAction方法添加需要接受的廣播的值。
3.動態(tài)注冊的Broadcast Receiver最后一定要取消注冊。
class MainActivity : AppCompatActivity() {
lateinit var timechange:TimeChange
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val intentFilter= IntentFilter()
intentFilter.addAction("android.intent.action.TIME_TICK")
timechange=TimeChange()
registerReceiver(timechange,intentFilter)
}
override fun onDestroy(){
super.onDestroy()
unregisterReceiver(timechange)
}
inner class TimeChange:BroadcastReceiver(){
override fun onReceive(p0: Context?, p1: Intent?) {
Toast.makeText(p0,"Time Change",Toast.LENGTH_SHORT).show()
}
}
}
靜態(tài)注冊:在AndroidManifest.xml中注冊
靜態(tài)注冊只能監(jiān)聽到少量特殊的系統(tǒng)廣播(顯式廣播)
步驟
右擊com.example.broadcasttest包->"New"->"other"->"Broadcast Recevier"。
會彈出窗口,選擇Exported(屬性表示是否允許這個BroadcastRecevier接收本程序以外的廣播)
選擇Enabled(屬性表示是否啟用這個BroadcastRecevier)輸入類名,會自動創(chuàng)建BroadcastRecevier的子類和在AndroidManifest.xml里面注冊,這時,我們只要重寫onRecevier方法和在AndroidManifest.xml里注冊的recevier添加標(biāo)簽和action就行,最后記得思考是否需要進(jìn)行權(quán)限聲明。
class Boot : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Toast.makeText(context,"Boot",Toast.LENGTH_SHORT).show()
}
}
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> ..... <receiver android:name=".Boot" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"></action> </intent-filter> </receiver>
二、自定義廣播
標(biāo)準(zhǔn)廣播:全部一起接收廣播消息,不能被截斷
1.方法解析
packageName:是getpackageName的語法糖,用于獲取當(dāng)前程序的包名
setPackage():參數(shù)為packageName(程序的包名),傳入當(dāng)前程序的包名給intent,指定這條廣播發(fā)送給哪個應(yīng)用程序,把它從隱式廣播變成顯式廣播
sendBroadcast():將廣播發(fā)出去
2.發(fā)送標(biāo)準(zhǔn)廣播
步驟
1.新建一個BroadcastRecevier的子類(詳細(xì)方法在靜態(tài)注冊里)
class MyReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Toast.makeText(context,"MyReceiver",Toast.LENGTH_SHORT).show()
}
}
2.AndroidManifest.xml里注冊的recevier添加標(biāo)簽和action
<receiver android:name=".MyReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.example.MyApplication.MY_BROADCAST"/> </intent-filter> </receiver>
3.在布局文件中加入button
<Button android:id="@+id/send" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="send" />
4.在button的點擊事件中,構(gòu)建intent對象,并把要發(fā)送的廣播值傳入,把它變成顯式廣播,然后把它發(fā)送出去。
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val send:Button=findViewById(R.id.send)
send.setOnClickListener {
val intent=Intent("com.example.MyApplication.MY_BROADCAST")
intent.setPackage(packageName)
sendBroadcast(intent)
}
}
}
有序廣播:廣播按照規(guī)定的次序,一個一個地接收廣播消息,可截斷。
1.方法解析
sendOrderedBroadcast():發(fā)送廣播,由兩個參數(shù),第一個參數(shù)式intent,第二個參數(shù)是一個與權(quán)限相關(guān)地字符
abortBroadcast():將這條廣播截斷,后面地BroadcastRecevier無法接收廣播消息
android:priority屬性給BroadcastRecevier設(shè)置優(yōu)先級
2.發(fā)送有序廣播
步驟
1.新建兩個BroadcastRecevier的子類
class MyReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Toast.makeText(context,"MyReceiver",Toast.LENGTH_SHORT).show()
abortBroadcast()
}
}
class MyReceiver2 : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Toast.makeText(context,"MyReceiver2", Toast.LENGTH_SHORT).show()
}
}
2.AndroidManifest.xml里注冊的recevier添加標(biāo)簽和action,并設(shè)置優(yōu)先級
<receiver android:name=".MyReceiver2" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.example.MyApplication.MY_BROADCAST" /> </intent-filter> </receiver> <receiver android:name=".MyReceiver" android:enabled="true" android:exported="true"> <intent-filter android:priority="100"> <action android:name="com.example.MyApplication.MY_BROADCAST" /> </intent-filter> </receiver>
3.在布局文件中加入button
<Button android:id="@+id/send" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="send" />
4.在button的點擊事件中,構(gòu)建intent對象,并把要發(fā)送的廣播值傳入,把它變成顯式廣播,然后把它發(fā)送出去。
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val send:Button=findViewById(R.id.send)
send.setOnClickListener {
val intent=Intent("com.example.MyApplication.MY_BROADCAST")
intent.setPackage(packageName)
sendOrderedBroadcast(intent,null)
}
}
}
原文鏈接:https://blog.csdn.net/weixin_63357306/article/details/126505256
相關(guān)推薦
- 2023-01-02 Android?Map數(shù)據(jù)結(jié)構(gòu)全面總結(jié)分析_Android
- 2022-04-07 Redis數(shù)據(jù)庫分布式設(shè)計方案介紹_Redis
- 2023-05-21 C#?Replace替換的具體使用_C#教程
- 2022-12-19 Python?完美解決?Import?“模塊“?could?not?be?resolved?...的
- 2022-06-28 Python實現(xiàn)單鏈表中元素的反轉(zhuǎn)_python
- 2022-05-21 基于C++實現(xiàn)信息管理系統(tǒng)_C 語言
- 2022-09-24 React報錯之Object?is?possibly?null的問題及解決方法_React
- 2022-07-07 Python常用內(nèi)置函數(shù)和關(guān)鍵字使用詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支