android的每個程式都可以對自己感興趣的廣播進行註冊,這樣改程式就只會接收到自己所關心的廣播內容(感覺跟mqtt很像)
廣播分為兩種型別:標準廣播和有序廣播
廣播的註冊
廣播的註冊有兩種方式
1、首先新建乙個類繼承broadcastreceiver,重寫onreceive(),onreceive中寫對於接收到廣播的處理操作(我這裡我列印了乙個吐司)
class
networkchangereceiver
extends
broadcastreceiver
}
2、建立intentfilter例項,新增相應的監聽,呼叫registerreceiver()
intentfilter=
newintentfilter()
; intentfilter.
addaction
("android.net.conn.connectivity_change");
networkchangereceiver=
newnetworkchangereceiver()
;registerreceiver
(networkchangereceiver,intentfilter)
;
3、最後不要忘記呼叫unregisterreceiver()來取消註冊,在ondestroy方法中寫
@override
protected
void
ondestroy()
完整**如下:
package com.example.helloactivity;
import android.content.broadcastreceiver;
import android.content.context;
import android.content.intent;
import android.content.intentfilter;
import android.os.bundle;
import android.widget.toast;
public
class
testbroadcast
extends
@override
protected
void
ondestroy()
class
networkchangereceiver
extends
broadcastreceiver
}}
例項:
1、首先我們先建立乙個廣播接收器
修改廣播接收器**:
package com.example.helloactivity;
import android.content.broadcastreceiver;
import android.content.context;
import android.content.intent;
import android.widget.toast;
public
class
myreceiver
extends
broadcastreceiver
}
2、然後在androidmanifest.xml中進行註冊(如果是上述as快捷生成的則不用了,它給自動配置)
android:name=
".myreceiver"
android:enabled=
"true"
android:exported=
"true"
>
<
/receiver>
3、監聽開機廣播是需要相應許可權的,賦予許可權
搞定自定義廣播
自定義標準廣播
實現:1、首先我們還是先建立乙個廣播接收器(同上面)
2、然後在androidmanifest.xml中對廣播進行配置
android:name
=".myboardcastreceiver"
android:enabled
="true"
android:exported
="true"
>
>
android:name
="com.example.helloactivity.my_boradcast"
>
action
>
intent-filter
>
receiver
>
3、然後傳送廣播
傳送廣播邏輯:
3.1、首先構建乙個intent物件
3.2、呼叫sendbroadcast將廣播傳送出去
廣播是通過intent傳遞的,intent中還可以攜帶資料傳遞給廣播接收器
intent intent9=
newintent
("com.example.helloactivity.my_boradcast");
sendbroadcast
(intent9)
;
傳送自定義有序廣播
有序廣播基於標準廣播的基礎上
第一步第二步
都是相同的
3、使用sendorderbroadcast()方法傳送廣播
intent intent10=
newintent
("com.example.helloactivity.order_boardcast");
sendorderedbroadcast
(intent10,null)
;
其中第二個引數是有關許可權的東西
到此其實這個廣播就已經可以用了,但是還沒有體現有序廣播的先後順序
修改< intent-filter >標籤內容,設定優先順序:
android:name=
".order_broadcast"
android:enabled=
"true"
android:exported=
"true"
>
"100"
>
"com.example.helloactivity.order_boardcast"
>
<
/action>
<
/intent-filter>
<
/receiver>
再體現有序廣播的攔截功能,在廣播接收器中新增:
package com.example.helloactivity;
import android.content.broadcastreceiver;
import android.content.context;
import android.content.intent;
import android.widget.toast;
public
class
order_broadcast
extends
broadcastreceiver
}
搞定
###################################分割線######################################
以上的廣播全部屬於系統全域性廣播,下面搞一下本地廣播,即本應用程式單獨使用的廣播機制,本地廣播用法並不複雜,使用了乙個localbroadcastmanager來對廣播進行管理,並提供了傳送和註冊廣播接收器的方法
android 廣播機制
1 首先說andoid 廣播分為系統的和 自定義的 2 註冊方式呢,也是兩種,1 靜態註冊,在manifest.xml 檔案中註冊的 2 動態註冊,用filter 區分 不說了 佔 首先是動態註冊 broadreceiver re new broadreceiver 自己定義的接收器 intentf...
Android基礎 廣播
broadcast receiver 廣播算是比較特殊的乙個元件 它在使用的時候有兩種註冊模式 一種是靜態註冊 一種是動態註冊 靜態註冊就是在清單檔案中註冊 這種註冊方法有些頻繁使用的廣播是不支援靜態註冊的 因為每次都要便利清單檔案 同時 廣播的觸發條件又較為頻繁 如螢幕是否關閉等 這種註冊的廣播持...
Android定向廣播
有時候我們在使用廣播的時候不希望自己傳送的廣播被其他應用程式接收到,那怎麼辦呢?我們只能定向的傳送廣播。本篇博文介紹一下定向廣播的使用。在進入正題之前先來介紹一下android ssp這個屬性。這個是google在api level 19加入的乙個xml 屬性,用於intent過濾的標籤中。ssp是...