The use of radio in Android

1. Dynamic broadcast and static broadcast

Static broadcast

需要在AndroidManifest.xml文件中配置, Has 常驻性广播,无论程序启动与否,广播依然存在the characteristics. For example: we can receive the broadcast of booting, and when we receive this broadcast, we can execute the corresponding logic in the onReceive() method to realize the function of booting.

Dynamic broadcast

The broadcast address is dynamically designated and registered in the code, which has 非常驻型,广播会跟随程序的生命周期的结束而结束the characteristics. Require special attention are : need onCreate()to register, the onDestroy()write-off of.

 

2. Orderly broadcast and standard broadcast

Orderly broadcast

It is a synchronous broadcast. After the broadcast is sent, only one broadcast receiver can receive the message at the same time . When the logic in the broadcast is executed, the broadcast will continue to spread.

Standard broadcast

It is transmitted in an asynchronous manner. After the broadcast is sent out, all broadcast receivers receive the message at almost the same time . There is no order between them, and this kind of broadcast cannot be truncated.

 

3. Global broadcast and local broadcast

Global broadcast:

Global broadcast means that the broadcast can be received by any other application, or can be received from any other application.

Local broadcast:

Local broadcast is a broadcast that can only be delivered within the application. The broadcast receiver can only receive internal broadcasts, and cannot accept broadcasts from other applications.

 

4. Code

Parameter preparation

public static final String BROADCAST_ACTION="com.test.TestBroadcast";
private TestBroadcastReceiver receiver;
private TempReceiver receiver2;
private LocalBroadcastManager localBroadcastManager;

Register the broadcast, here I take two receivers as an example, pay attention to call in oncreate

private void registerBroadcast(){
        IntentFilter filter=new IntentFilter();
        filter.addAction(BROADCAST_ACTION);
        receiver=new TestBroadcastReceiver();
        receiver2=new TempReceiver();
        localBroadcastManager=LocalBroadcastManager.getInstance(this);
        localBroadcastManager.registerReceiver(receiver,filter);
        localBroadcastManager.registerReceiver(receiver2,filter);
    }

sender:

Click the click event of a button in Activity and write it in the click event. Choose different types of broadcast according to your needs. I choose local standard broadcast here.

Intent intent=new Intent(DATA_REFRESH_BROADCAST_ACTION);
//sendBroadcast(intent);//发送标准广播
//sendOrderedBroadcast(intent,null);//发送有序广播
localBroadcastManager.sendBroadcast(intent);//发送本地标准广播
//localBroadcastManager.sendBroadcastSync(intent);//发送本地有序广播
@Override
protected void onDestroy() {
     super.onDestroy();
     localBroadcastManager.unregisterReceiver(receiver);//注销本地广播
     localBroadcastManager.unregisterReceiver(receiver2);
}

 

receiver:

public class TestBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("receiver", "onReceive: "+"收到广播");
    }
}
public class TempReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("receiver2", "onReceive: "+"收到广播");
    }
}

 

Guess you like

Origin blog.csdn.net/hzkcsdnmm/article/details/107614229