Android basic knowledge comb – four components of BroadcastReceiver
define
Broadcast: A mechanism widely used for transmitting information between applications;
BroadcastReceiver: a component used to receive and respond to broadcasts from systems and other applications.
registered
1. Define the broadcast receiver
Inherit BroadcastReceiver, implement custom BroadcastReceiver, implement onReceive() method for receiving broadcast
public class ExampleBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = ExampleBroadcastReceiver.class.getSimpleName();
private static final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";
@Override
public void onReceive(Context context, Intent intent) {
if (ACTION_BOOT.equals(intent.getAction())) {
Log.i(TAG, "receive boot completed broadcast");
Toast.makeText(context, "system boot completed.", Toast.LENGTH_LONG).show(); }}}Copy the code
When the onReceive() method completes, the ExampleBroadcastReceiver lifecycle may be reclaimed by the system, so asynchronous operations are not recommended in onReceive(); Also, due to ANR constraints, the onReceive() method must be executed within 10 seconds.
2. Static registration
Register in androidmanifest.xml
<manifest .>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application .>
<receiver
android:name=".broadcast.ExampleBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
Copy the code
- Static broadcast registration allows applications to receive broadcasts even if they are not started. Therefore, broadcasts can be used as the self-start and keepalive mechanism of applications.
- Android :name: indicates a customized broadcast receiver class
- Action: Specifies the type of broadcast to be received
- Uses-permission: declares that we need to use the permission to receive the boot status
3. Dynamic registration
public class ExampleActivity extends AppCompatActivity {
private ExampleBroadcastReceiver myReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myReceiver = new ExampleBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.BOOT_COMPLETED");
registerReceiver(myReceiver, filter);
}
@Override
protected void onDestroy(a) {
super.onDestroy(); unregisterReceiver(myReceiver); }}Copy the code
- In the Activity
onCreate()
Through theregisterReceiver
Register broadcasts and specify the type of broadcasts to listen to; The lastIn onDestory ()
To turn off broadcast listening - Register broadcast in dynamic mode. Users can register broadcast when they need to use it, and turn off broadcast monitoring when they are no longer in use, which is more flexible in use
classification
Broadcast can be divided into disordered broadcast (default), ordered broadcast, local broadcast and Sticky broadcast.
A disorderly radio
An out-of-order broadcast is an asynchronous execution in which all receivers receive the broadcast almost at the same time, in no order, until the broadcast ends when no receivers receive the broadcast.
-
Send broadcast
public void sendCustomBroadcast(View view){ Intent intent = new Intent("com.example.broadcast"); sendBroadcast(intent); } Copy the code
Orderly broadcast
Ordered broadcast is a synchronous execution mode. After a broadcast is sent, only one receiver is receiving the broadcast at the same time. The broadcast executor receives the broadcast according to the priority order, and can cut off the broadcast during the receiving process, so that subsequent receivers cannot receive the broadcast.
-
Specifies the priority of the broadcast receiver
<manifest ... > <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <application ... > <receiver android:name=".broadcast.ExampleBroadcastReceiver" android:enabled="true" android:exported="true"> <intent-filter android:priority="100"> <action android:name="com.example.broadcast"/> </intent-filter> </receiver> </application> </manifest> Copy the code
Android :priority specifies the priority in IntentFilter. A larger value indicates a higher priority
-
Send broadcast
public void sendOrderBroadcast(View view){ Intent intent = new Intent("com.example.broadcast"); sendOrderBroadcast(intent); } Copy the code
-
Truncation radio
@Override public void onReceive(Context context, Intent intent) { abortBroadcast(); } Copy the code
In the broadcast receiver’s onReceive() method, the abortBroadcast() method can be called to truncate the broadcast so that lower priority broadcast receivers can no longer receive the broadcast
Local radio
Local broadcast is a local broadcast that can be broadcast only within an application, and the broadcast receiver can receive only the broadcast within the application. Therefore, static registration is not supported for local broadcast.
-
Register local broadcast receivers
public class ExampleActivity extends AppCompatActivity { private ExampleBroadcastReceiver myReceiver; private LocalBroadcastManager localBroadcastManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myReceiver = new ExampleBroadcastReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction("android.intent.action.BOOT_COMPLETED"); localBroadcastManager = LocalBroadcastManager.getInstance(this); localBroadcastManager.registerReceiver(intent, filter); } @Override protected void onDestroy(a) { super.onDestroy(); localBroadcastManager.unregisterReceiver(myReceiver); }}Copy the code
Through LocalBroadcastManager. GetInstance () or local radio management instance, use the instance to register the broadcast receiver
-
Send broadcast
private void sendBroadcast(a) { Intent intent = new Intent("com.example.broadcast"); localBroadcastManager.sendBroadcast(intent); } Copy the code
Sticky broadcast
From the broadcast types we have seen, we can see that the broadcast ends when all broadcast receivers have processed the broadcast reception. That is, broadcast receivers can only receive broadcasts that have been registered before they are sent. So what if we are in a situation where the receiver of the broadcast is registered after the broadcast sent time, but we want to receive the broadcast? That’s where sticky radio comes in.
-
Sticky broadcast saves the broadcast that was just sent after it is sent. When newly registered receivers register, they can continue to use the broadcast until the broadcast is removed.
-
Sticky broadcast saves only the last broadcast, that is, if multiple broadcasts are sent before a receiver registers, the receiver can only receive the last broadcast after registration.
-
Sticky broadcast sendStickyBroadcast() sends broadcast, removeStickyBroadcast() removes broadcast, And you need to add permissions < USES – permission android: name = “android. Permission. BROADCAST_STICKY” / >