日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android四大組件之broadcast廣播使用講解_Android

作者:落雪小軒韓 ? 更新時間: 2023-01-31 編程語言

一、廣播機制概述

通常情況下在學校的每個教室都會裝有一個喇叭,這些喇叭是接入到學校廣播室的。如果有重要通知,會發送一條廣播來告知全校師生。為了便于發送和接收系統級別的消息通知,Android系統也引入了一套類似廣播的消息機制。

Android中的廣播(Broadcast)機制用于進程/線程間通信,該機制使用了觀察者模式,觀察者模式是一種軟件設計模式,該模式是基于消息的發布/訂閱事件模型,該模型中的消息發布者是廣播機制中的廣播發送者,消息訂閱者是廣播機制中的廣播接收者。

上述圖中的廣播機制的實現流程具體如下:

1、廣播接收者是通過Binder機制在AMS(Activity Manager Service)中進行注冊的 (在8.2小節會講解廣播接收者的注冊)。

2、廣播發送者是通過Binder機制向AMS發送廣播。

3、AMS查找符合相應條件(IntentFilter/Permission)的廣播接收者 (BroadcastReceiver),將廣播發送到相應的消息循環隊列中。

4、執行消息循環時獲取到此廣播,會回調廣播接收者(BroadcastReceiver)中的 onReceive()方法并在該方法中進行相關處理。

二、廣播接收者

Android系統中內置了很多廣播,例如手機開機完成、電池電量不足時都會發送一條廣播。

為了監聽來自系統或者應用程序的廣播事件,Android系統提供了BroadcastReceiver(廣播接收者)組件。 當Android系統產生一個廣播事件時,可以有多個對應的廣播接收者接收并進行處理。

1、廣播接收者的創建

1、通過在應用程序的包中創建一個類繼承BroadcastReceiver并重寫onReceive()方法來實現

2、通過選中應用程序中的包,右擊選擇【New】→【Other】→【Broadcast Receiver】選項來創建

public class MyBroadcast extends BroadcastReceiver {
    @Override
    // 當廣播接收者接收廣播成功,此方法會自動回調執行,在該方法中實現廣播接收者的相關操作
    public void onReceive(Context context, Intent intent) {
    }
}

注意:創建完廣播接收者之后還需要對廣播接收者進行注冊才可以接收廣播。

2、廣播接收者的注冊

(1)靜態注冊

AndroidManifest.xml清單文件中

<receiver
   android:name=".utils.MyBroadcast"
   android:enabled="true"
   android:exported="true">
   <intent-filter>
   	<action>cn.com.hello</action> 
   </intent-filter>
</receiver>

注意:靜態注冊廣播,在小于Android8.0的設備上,只要設備處于開啟狀態,廣播接收者就能接收到廣播。Android8.0之后不支持靜態注冊方式。

(1)動態注冊

<receiver
   android:name=".utils.MyBroadcast"
   android:enabled="true"
   android:exported="true">
</receiver>
public class BroadcastReceiverActivity extends AppCompatActivity {
    private MyBroadcast myBroadcast;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broadcast_receiver);
        // 創建廣播接收者對象
        myBroadcast = new MyBroadcast();
        String action = "cn.com.hello"
        // 創建意圖過濾器
        IntentFilter intentFilter = new IntentFilter();
        // 設置要過濾的廣播
        intentFilter.addAction(action );
        // 注冊廣播接收者
        registerReceiver(myBroadcast,intentFilter);
    }
    // 廣播接收者是稀缺資源,使用結束要注銷,可以在app銷毀的階段進行注銷
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myBroadcast);
    }
}

注意:動態注冊的廣播接收者是否被注銷依賴于注冊廣播的組件,當組件銷毀時,廣播接收者也隨之被注銷。

三、廣播的類型

1、無序廣播

無序廣播是完全異步執行,發送廣播時所有監聽這個廣播的廣播接收者都會接收到此消息,但接收的順序不確定。

發送廣播:

btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
      // 發送一次廣播
      Intent intent = new Intent();
      intent.putExtra("info","hello,who are you ?");
      // 設置廣播的action,相當于廣播的名稱
      String action = "cn.com.hello"
      intent.setAction(action);
      // 發送廣播,廣播會發送到信息中心,由信息中心負責找到這個廣播的訂閱者并將廣播的意圖傳過去
      // 無序廣播
		 sendBroadcast(intent);
  }
});

接收廣播:

(1)MyBroadcast工具類文件:

public class MyBroadcast extends BroadcastReceiver {
    @Override
    // 當廣播接收者接收廣播成功,此方法會自動回調執行
    public void onReceive(Context context, Intent intent) {
        String msg = intent.getStringExtra("info");
			 Log.v("msg","接收到了廣播信息!");
    }
}

(2)Activity文件

myBroadcast = new MyBroadcast();
String action = "cn.com.hello"
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(action); 
registerReceiver(myBroadcast,intentFilter);

2、有序廣播

按照接收者的優先級接收,只有一個廣播接收者能接收消息,在此廣播接收者中邏輯執行完畢后,才會繼續傳遞。

發送廣播:

Intent intent = new Intent();
intent.putExtra("info","hello,who are you ?");
String action = "cn.com.hello"
intent.setAction(action);
// 有序廣播
sendOrderedBroadcast(intent,null);

接收廣播:

(1)MyBroadcast工具類文件:

public class MyBroadcast extends BroadcastReceiver {
    @Override
    // 當廣播接收者接收廣播成功,此方法會自動回調執行
    public void onReceive(Context context, Intent intent) {
        String msg = intent.getStringExtra("info");
			 Log.v("msg","接收到了廣播信息!");
			 // 截斷廣播的發送,之后的廣播接收者就接收不到廣播了
        abortBroadcast();
    }
}

(2)Activity文件

myBroadcast = new MyBroadcast();
String action = "cn.com.hello"
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(action); 
// 設置優先級
intentFilter.setPriority(8);
// 注冊廣播接收者
registerReceiver(myBroadcast,intentFilter);

注意:數值越大,優先級越高。如果兩個廣播接收者的優先級相同,則先注冊的廣播接收者優先級高。

原文鏈接:https://blog.csdn.net/m0_46613429/article/details/128278164

欄目分類
最近更新