1. The types of broadcasting can be mainly divided into ordered broadcasting and standard broadcasting.
A standard broadcast is a broadcast that is executed completely asynchronously. After it is sent, all broadcast receivers will receive the broadcast message at almost the same time.
Ordered broadcast is a synchronous broadcast. After the broadcast is sent, only one broadcast receiver can receive the broadcast message at the same time.
Second, the broadcast receiver is divided into static registration and dynamic registration
Dynamic registration listener
1. Create a class that inherits from BroadcastReceiver (assume A) and override onReceive().
2. Create an instance of IntentFilter (assuming b). And add the action of android.net.conn.CONNECTIVITY_CHANCE to him
3. Create an object a of A
4. Call registerReceiver(a, b); register the broadcaster.
code example
........
private IntentFilter intentFilter;
private A a;
@Override
protected void onCreated(Bundle savedInstanceState){
super.onCreate (savedInstanceState);
setContentView(R.layout.activity_main);
intentFilter=new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
a = new A();
registerReceiver(a,intentFilter);
}
class A extends BroadcastReceiver{
@Override
public void onReceive(Context context,Intent intent){
@Override
public void onReceive(Context context,Intent intent){
Toast.makeText(context,"network changes",Toast.LENGTH_SHORT).show();
}
}
Precautions:
Dynamically registered broadcast receivers must be unregistered. That is, call unregisterReceiver() in the onDestroy() method
Code example:
@Override
protected void onDestroy(){
super.onDestroy ();
unregisterReceiver(a);
}
static registration
Static broadcasters do not require any objects to be created in onCreate.
Just create a corresponding class that inherits from BroadcastReceiver, and then override the onReceive() method.
Static broadcasters must be registered in the AndroidManifest.xml file before they can be used.
Need to be added in the <application> tag
<receiver> tag
Sample code:
<receiver
android:name=".BootCompleteReceiver”
android:enabled="true" //Whether this broadcaster is enabled
android:exported="true"> //Whether this broadcaster is allowed to accept broadcasts other than this program
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETE"/> //Add the action to be accepted to the broadcaster
</intent-filter>
</receiver>
3. Attention
In order to protect the security and privacy of user devices, the Android system has made strict regulations: if the program needs to perform some operations that are sensitive to users, it must declare permissions in the configuration file, otherwise it will crash directly.
That is, in the AndroidManifest file, add the following permissions to it:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>//What is added here is the state permission to access the system network state.
in addition
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED "/>//The permission to access the boot state is added here.