classification
Standard Broadcasting
Receive at the same time, can not be truncated
Orderly broadcasting
High priority first receives broadcasts and can be truncated
Registration method
Dynamic registration
Register in code
ps: It can only receive broadcasts when the program starts
1. Create a new class that inherits from BroadcastReceiver and overrides the parent's onReceive() method as the body of logical processing for receiving broadcasts
class NetworkChangeReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "network changes", Toast.LENGTH_SHORT).show(); } }
2. Next, create IntentFilter object and BroadcastReceiver object in onCreate(), and call registerReceiver() broadcasting registration method.
IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); NetworkChangeReceiver networkChangeReceiver = new NetworkChangeReceiver(); registerReceiver(networkChangeReceiver, intentFilter);
3. Finally, cancel the broadcast in onDestroy()
protected void onDestroy(){ super.onDestroy(); unregisterReceiver(netwrokChangeReceiver); }
Static registration
Register in Android Manifest. XML
ps: Programs can receive broadcasts without startup
1. Like dynamic registration, we need to create a new class to inherit BroadcastReceiver and implement the method onReceive()
2. Register in the Android Manifest. XML file (an activity-like operation)
//BOOT_COMPLETED is the action of boot broadcasting <receiver android:name=".BootCompleteReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver>
Custom Broadcasting
1. Choose dynamic or static registration mode for registration.
<receiver android:name=".MyBroadcastReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.example.broadcasttest.MY_BROADCAST"/> </intent-filter> </receiver>
2. Broadcasting via Intent
Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST"); sendBroadcast(intent);
Send orderly broadcasting (different from standard broadcasting)
1. The method changes from sendBroadcast(intent) to sendOrdered Broadcast (intent, null);
Intent("com.example.broadcasttest.MY_BROADCAST"); sendOrderedBroadcast(intent, null);
2. Set priority (add label priority= "100" in intent-filter)
<intent-filter android:priority="100"> <action android:name="com.example.broadcasttest.MY_BROADCAST" /> </intent-filter>
3. Truncation of broadcasting
//Truncation in the onReceive() method in Broadcast Receiver abortBroadcast();
Local Broadcasting (in-program)
ps: Local broadcasting can not be received by static registration, because the program has been started when it is transmitted inside the program.
1. Get the LocalBroadcast Manager object
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this); // Get an instance
2. Transmit and cancel broadcasts through the LocalBroadcast Manager object
localBroadcastManager.sendBroadcast(intent); localBroadcastManager.unregisterReceiver(localReceiver);
Broadcasting receivers are more likely to play the role of opening other components of the program, such as creating a status bar notification or starting a
No threads can be opened in onReceive().