自定義發(fā)送廣播

廣播主要分為兩種類型:標(biāo)準(zhǔn)廣播(完全異步執(zhí)行的廣播)和有序廣播(完全同步執(zhí)行的廣播)。我們也可以根據(jù)廣播是否可以跨進(jìn)程接收,將廣播分為:系統(tǒng)全局廣播和本地廣播

  • 系統(tǒng)全局廣播:發(fā)出的廣播可以被其他任何程序接收到,并且我們也可以接收來自其他任何應(yīng)用程序的廣播
  • 本地廣播:發(fā)出的廣播只能在應(yīng)用程序的內(nèi)部進(jìn)行傳遞,廣播接收器也只能接收本應(yīng)用程序發(fā)出的廣播

發(fā)送系統(tǒng)全局廣播


  • 標(biāo)準(zhǔn)廣播
    發(fā)送廣播借助的是Intent對(duì)象,使用的是Context對(duì)象的sendBroadcast()方法發(fā)送廣播。
    代碼實(shí)現(xiàn):
//構(gòu)建一個(gè)Intent對(duì)象,傳入要傳遞的廣播
Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
//調(diào)用sendBroadcast方法發(fā)送出標(biāo)準(zhǔn)廣播
sendBroadcast(intent);
  • 有序廣播
    發(fā)送有序廣播和發(fā)送標(biāo)準(zhǔn)廣播思路基本一致,發(fā)送有序廣播時(shí),調(diào)用的是Context對(duì)象的sendOrderedBroadcast()方法:
Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
//發(fā)送有序廣播,第二個(gè)參數(shù)是與權(quán)限相關(guān)的字符串,傳入null即可
sendOrderedBroadcast(intent, null);

發(fā)送有序廣播時(shí),進(jìn)程接收廣播是有順序的,并且可以劫持廣播,設(shè)置廣播接收優(yōu)先級(jí):在配置文件中配置<receiver>標(biāo)簽時(shí),給<intent-filter>標(biāo)簽添加屬性"android:priority="xxx"",實(shí)現(xiàn)攔截廣播,調(diào)用方法:abortBroadcast()

發(fā)送本地廣播


發(fā)送本地廣播主要使用LocalBroadcastManager來對(duì)廣播進(jìn)行管理,并提供了發(fā)送廣播和注冊(cè)廣播接收器的方法

代碼實(shí)現(xiàn):

public class MainActivity extends Activity {
    
    private IntentFilter intentFilter;
    
    private LocalReceive localReceive;
    
    private LocalBroadcastManager localBroadcastManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //獲取到LocalBroadcastManager實(shí)例對(duì)象
        localBroadcastManager = LocalBroadcastManager.getInstance(this);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                //創(chuàng)建Intent對(duì)象,指定廣播內(nèi)容
                Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
                //發(fā)送本地廣播
                localBroadcastManager.sendBroadcast(intent);
            }
        });
        intentFilter = new IntentFilter();
        intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST");
        localReceive = new LocalReceive();
        //注冊(cè)本地廣播接收器
        localBroadcastManager.registerReceiver(localReceive, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //動(dòng)態(tài)注冊(cè),同樣需要取消注冊(cè)廣播
        localBroadcastManager.unregisterReceiver(localReceive);
    }
    
    class LocalReceive extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "received local broadcast", Toast.LENGTH_SHORT).show();
        }
        
    }

}

本地廣播是無法通過靜態(tài)注冊(cè)的方式來接收的,因?yàn)殪o態(tài)注冊(cè)主要就是為了讓程序在未啟動(dòng)的情況下也能收到廣播,而發(fā)送本地廣播時(shí),我們的程序肯定已經(jīng)啟動(dòng)了

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

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

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