Android BroadcastReceiver broadcast (1)

What is BroadcastReceiver?

As one of the four basic components of Android, BroadcastReceiver can easily realize the communication between different basic components and different application processes. It can monitor global broadcast information, which can be sent by programs developed by developers themselves, or Sent by the Android system program, BroadcastReceiver can monitor and receive these broadcast messages.

What is the meaning of BroadcastReceiver?

Let’s take another example of scenario use: when the Service is started in the Activity, the Activity and the Service often need to transfer data during the running process, for example, the Activity controls whether the music player in the Service is playing, and the Service transmits data downloads to the Activity at regular intervals In order for the Activity to update the UI interface, etc., at this time, the data transfer between different basic components can be realized more conveniently through the BroadcastReceiver.

【Send broadcast information】

Two ways to send broadcast information

  • The first one is: send ordinary broadcast
  • Meaning: All receivers can receive the broadcast information at the same time, but cannot modify the broadcast information.
  Intent intent = new Intent();
  intent.setAction("ccv.turbosnail.broadcastdemo");	//接收方匹配 Action 才能接收广播
  intent.putExtra("data","普通广播信息");
  sendBroadcast(intent);				//	发送 普通 广播
  • The second is: sending ordered broadcasts
  • Meaning: broadcast receivers with high priority receive first, and those with low priority receive later. Broadcast receivers with high priority can control the broadcast to stop spreading, and can also modify the content of the broadcast message
  Intent intent = new Intent();
  intent.setAction("ccv.turbosnail.broadcastdemo");	//接收方匹配 Action 才能接收广播
  intent.putExtra("data","普通广播信息");
  sendOrderedBroadcast(intent,null);	//	发送 有序 广播

[Receiving broadcast information]
1.
The broadcast receiver BroadcastReceiver object is used to monitor and receive broadcast information. After receiving the corresponding broadcast, it will automatically call back the onReceive() method. Therefore, the processing after receiving broadcast information and the interaction with other components are generally Both can be placed in the onReceive() method.

   @Override
    public class MyReceiver extends BroadcastReceiver{
    
    
        @Override
        public void onReceive(Context context, Intent intent) {
    
    
            String data = intent.getStringExtra("data");
            Toast.makeText(ReceiverActivity.this,data,Toast.LENGTH_LONG).show();
        }
    }

How to modify the obtained value:

Bundle bundle = new Bundle();
bundle.putString("data",data + "追加的数据");
setResultExtras(bundle);

Register broadcast receiver (two ways):
Method 1:

可以在AndroidManifest.xml 里通过<receive> 标签注册自定义的广播接收器
	  <receiver android:name=".ReceiverActivity$MyReceiver">
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
            </intent-filter>
      </receiver>

For orderly broadcasting, it is necessary to add a priority attribute description to the label. The value range of the priority is [-1000,1000]. The larger the value, the higher the priority. The specific writing method is as follows:

  <intent-filter android:priority="900"></intent-filter>

Method 2:

        MyReceiver myReceiver = new MyReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("ccv.turbosnail.broadcastdemo");
        registerReceiver(myReceiver,intentFilter);

Register through java code, but be careful! Remember to unregister when you're done using it! code show as below:

unregisterReceiver(myReceiver);

In general, it is recommended to register the broadcast receiver in the onResume() method and unregister it in the onPause() method

Guess you like

Origin blog.csdn.net/qq_27494201/article/details/96360960#comments_26144152