BroadCast的初級使用手記

這只是我在學(xué)Android過程中對于所學(xué)知識的鞏固和方便日后查詢的學(xué)習(xí)筆記,能幫助到有需要的和我一樣的初學(xué)者就更好了

廣播分為標(biāo)準(zhǔn)廣播和有序廣播
標(biāo)準(zhǔn)廣播:所有接收器同時可以接收
有序廣播:同一時刻只能有一個接收器接收到,并且可以決定是否繼續(xù)向下傳播

Receiver可在代碼中注冊(動態(tài)注冊)也可在AndroidManifast.xml中注冊(靜態(tài)注冊)
動態(tài)注冊可在應(yīng)用運行時接收廣播
靜態(tài)注冊在任何時候都可以接收廣播(最常見即是防止服務(wù)關(guān)閉用來自啟)

首先

創(chuàng)建MyReceiver繼承BroadCastReceiver并重寫onReceive()即可

public class MyReceiver extends BroadCastReceiver{
        @override
        public void onReceive(Context context ,Intent intent){
        //業(yè)務(wù)邏輯
        }
}

動態(tài)注冊

以Activity中為例

MyReceiver receive;
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setonContentView(R.layout.activity_main);
    IntentFilter intentFilter=new IntentFilter();//動作捕捉
    intentFilter.addAction("......")//所要捕捉的動作
    receiver=new MyReceiver();
    registerReceiver(receiver ,intentFilter);
}
protected void onDestroy(){
    super.onDestroy();
    unregisterReceiver(receiver)
}

動態(tài)注冊后在銷毀活動時一定要解除注冊

靜態(tài)注冊

AndroidManifaste.xml中
<application>中
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:export="true"
<intent-filter>
<action android:name="......">//此為IntentFilter所要捕捉的動作,可自定
</intent-filter>
/>

發(fā)送自定義廣播

以自定的 "can you receive this broadcast"為例

標(biāo)準(zhǔn)廣播

這里采用靜態(tài)注冊的方式
onReceive()方法中添加一個Toast來表示接受到即可

AndroidManifast.xml中
<appication
    <reciever
        <intent-filter>
            <action android:name="can you receive this broadcast" />
        </intent-filter>
    />
/>

創(chuàng)建一個按鈕button模擬觸發(fā)

button.setonClickListener(....){
     public void onClick(View v){
          Intent intent=new Intent("can you receive this broadcast");
          sendBroadcast(intent);
    }   
}

此時點擊按鈕MyReceiver即可接收到此廣播

有序廣播

與標(biāo)準(zhǔn)廣播大體相似,只不過發(fā)送廣播的方法不同

sendOrderBroadcast(intent,null);
//第二個參數(shù)為與權(quán)限有關(guān)的字符串,一般null即可

然后氣<intent-filter>中設(shè)置優(yōu)先級即可

<intent-filter android:priority="100">

捕捉相同動作的receiver優(yōu)先級大的優(yōu)先接收sendOrderBroadcast()發(fā)送的廣播
在onReceive()方法中調(diào)用 abortBroadcast()可截斷廣播使之無法繼續(xù)傳遞

本地廣播

為避免自用的重要廣播如發(fā)送了全局廣播可能會被其他應(yīng)用截獲從而引起安全問題或者其他應(yīng)用不斷向自己發(fā)送垃圾廣播

LocalBroadcastManager manager;
MyReceiver receiver;
manager=LocalBroadcastManager.getInstance(this);
......
    void onClick(View v){
        Intent intent=new Intetn("can you receive this broadcast");
        manager.sendBroadcast(intent);
    }
     IntetnFilter filter=new IntentFilter();
     filter.addAction("can you receive this broadcast");
     reveiver=new MyReceiver();
     manager.registerReceiver(receiver,filter);
}
protevted void onDestroy(){
    super.onDestroy();
    manager.unregisterReveiver(receiver);
}

本地廣播無法使用靜態(tài)注冊來接收,因為發(fā)送本地廣播時應(yīng)用肯定已然啟動了

如有錯誤歡迎指正

結(jié)束

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容