Simply document the simple utility of a Broadcast Receiver
1. Broadcast classification
1.1 Normal Broadcasts Standard broadcasts
A fully asynchronous broadcast in which all broadcast receivers are broadcast almost simultaneously and cannot be truncated.
1.2 Ordered Broadcast
Obviously, in contrast to standard broadcast, ordered broadcast is ordered, the priority of each receiver can be set, can be truncated.
2. Register the global broadcast receiver
Full broadcast represents a broadcast that can be received by all broadcast receivers in the entire Android system
2.1 Dynamic registration of broadcast receivers
Receive system broadcast
The main steps are as follows:
- Custom BroadcastReceiver inherits BroadcastReceiver and overwrites the onReceive method
class NetWorkReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
networkInfo = connectivityManager.getActiveNetworkInfo();// Request permission
if(networkInfo ! =null && networkInfo.isAvailable())
Toast.makeText(context,"The network is on.",Toast.LENGTH_SHORT).show();
else
Toast.makeText(context,"The Internet is down.",Toast.LENGTH_SHORT).show(); }}Copy the code
- Create a new instance of a custom broadcast receiver in your code and register it in your Activity, passing in the type of broadcast the receiver accepts.
public class MainActivity extends AppCompatActivity {
private IntentFilter intentFilter;
private NetworkInfo networkInfo;
private NetWorkReceiver netWorkReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentFilter = new IntentFilter();
netWorkReceiver = new NetWorkReceiver();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");// Sets what the broadcast receiver accepts
registerReceiver(netWorkReceiver,intentFilter);// Register a broadcast receiver}}Copy the code
- Unregister the broadcast receiver in the Activity’s onDestroy method.
@Override
protected void onDestroy(a) {
super.onDestroy();
unregisterReceiver(netWorkReceiver);
}
Copy the code
At this point, a broadcast receiver is set up to receive the system broadcast. When the switch of the data network changes, the corresponding broadcast will be received and the Toast message will be sent.
2.2 Register static broadcast receivers
A static broadcast receiver also has the property of being able to accept and process broadcasts when the system is not started
2.2.1 Receiving System Broadcast (Startup)
- Also custom BroadcastReceiver inherit from BroadcastReceiver and override onReceive method
public class BootCompleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"It's on!",Toast.LENGTH_LONG).show(); }}Copy the code
- Register in the androidmanifest.xml file (automatically register if created using AS)
<application
>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> // broadcast receiver registration <receiver Android :name=".BootCompleteReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action
android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
</application>
Copy the code
2.3 Sending and receiving customized Broadcasts
Custom broadcasts use static registration
2.3.1 Sending standard broadcasts
- Broadcast receiver registration
The steps are the same as for static broadcast receivers, but when registering in the Androidmanifest.xml file, you should fill in the tag of the broadcast receivers with your own custom broadcast name
. <intent-filter> <action android:name="com.override0330.example"></action>
</intent-filter>
......
Copy the code
- To send a broadcast – to send with an Intent:
. Intent intent = new Intent("com.override0330.example"); sendBroadcast(intent); .Copy the code
2.3.2 Sending an Ordered broadcast
- Broadcast receiver registration
The procedure is the same as sending standard broadcast, but with an extra priority setting
. <intent-filter android:priority="98">// Set priority to 98 and Max to 100 <action Android :name="com.override0330.example"></action>
</intent-filter>
......
Copy the code
- Send ordered broadcast
Intent intent = new Intent("com.override0330.example"); sendOrderBroadcast(intent, null); // The null argument is a permission-related string. Null is normally passed inCopy the code
- Truncation radio
Use in onReceive for the broadcast receiver you want to intercept
abortBroadcast(); // Truncate the broadcastCopy the code
3. Use local broadcast
Local broadcast is different from global broadcast in that it is only passed in the application. If you have mastered global broadcast, the use of local broadcast is very simple. And it looks like local radio can only use standard radio. Change the method we used in the figure above to register broadcast receivers and send broadcasts to use the method provided by LocalBroadcastManager:
// Get the LocalBroadManage instance LocalBroadcastManagerlocalBroadcastManager = LocalBroadcastManager.getInstance(this); // Send a local broadcastlocalBroadcastManager.sendBroadcast(intent); // Register local broadcast receiverslocalBroadcastManager.registerReceiver(localBroadReceiver,intentFilter);
Copy the code
4. To summarize
Broadcasting is fairly simple to use, but its underlying implementation is something I have yet to learn √