Android BroadcastReceiver

Introduction to BroadcastReceiver

BroadcastReceiverIs one of the four components to complete communication between components or applications.

Broadcast: Radio broadcast

BroadcastReceiver: Broadcast receivers

A radio and broadcast receiver completes a message mechanism at the weight level because it needs to activate the component.

 

Two attention

Every time a broadcast message arrives in Android, an instance of BroadcastReceiver is created and the onReceive () method is executed

onReceive()After execution of the method, the instance of BroadcastReceiver will be destroyed.

onReceive()Method is also called by the main thread.

onReceive()If the method is not executed in 10s, it will lead to an abnormal ANR (application not response).

So in BroadcastReceiver, we can not do some time consuming operations.

If you need to complete a time-consuming operation, you should do so by sending Intent to Service by Service, which can’t be solved with a child thread here

Because the life cycle of BroadcastReceiver is very short, the sub-threads may have finished before the end of BroadcastReceiver

BroadcastReceiverAt the end of the process, the location of BroadcastReceiver is easy to kill when the system needs memory.

Because it belongs to an empty process (a process without any active components) and if its host process is killed, the working sub-threads are also killed, it is unreliable to adopt a sub-threading solution

 

Three disorderly broadcasting

There is no sequence of reception. All broadcast receivers simultaneously receive.

// Broadcast receivers
public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // When the broadcast receiver receives the broadcast, the method is called.
        if ("com.hy.mm.action.TEST".equals(intent.getAction())) {
            String name = intent.getStringExtra("name");
            Log.i("HUANG", "name=" + name);
        }
    }

}
// The first subscription broadcast mode is configured with receiver in the AndroidManifest.xml application node.
<receiver android:name=".receiver.MyBroadcastReceiver">
    <intent-filter>
        <action android:name="com.hy.mm.action.TEST" />
    </intent-filter>
</receiver>
// Second kinds of subscription broadcast modes
public class MainActivity extends AppCompatActivity {

    MyBroadcastReceiver mReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Subscribe to the broadcast
        mReceiver = new MyBroadcastReceiver();
        IntentFilter filter = new IntentFilter("com.hy.mm.action.TEST");
        registerReceiver(mReceiver, filter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // unsubscribe
        unregisterReceiver(mReceiver);
    }

}
// Sending out of order broadcasting
Intent intent = new Intent("com.hy.mm.action.TEST");
intent.putExtra("name", "Little white ");
sendBroadcast(intent);

 

Four ordered broadcast

The order of receiving is designated by the broadcasters themselves (-1000 ~ 1000).

If the received broadcast receiver interrupts the broadcast, the receiver will not be able to receive it later.

Special case: If an ordered broadcast has a specified receiver at the time it is sent, the uninterrupted broadcast must be sent to the designated receiver.

// Broadcast receiver 1
public class MyBroadcastReceiver1 extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // When the broadcast receiver receives the broadcast, the method is called.
        if ("com.hy.mm.action.T".equals(intent.getAction())) {
            int age = intent.getIntExtra("age", 0);
            Log.i("HUANG", "MyBroadcastReceiver1 age=" + age);
        }
    }

}
// Broadcast receiver 2
public class MyBroadcastReceiver2 extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // When the broadcast receiver receives the broadcast, the method is called.
        if ("com.hy.mm.action.T".equals(intent.getAction())) {
            int age = intent.getIntExtra("age", 0);
            Log.i("HUANG", "MyBroadcastReceiver2 age=" + age);
            // Interrupt broadcast//abortBroadcast();
        }
    }

}
// Subscribe to the broadcast AndroidManifest.xml application node, which is configured with receiver.
<!-- priority: The higher the priority value (-1000 ~ 1000), the higher the priority is -->< receiver android:name= ".Receiver.MyBroadcastReceiver1" >< intent-filter android:priority= "900" >< action android:name= "com.hy.mm.action.T" />< /intent-filter>< /receiver>< receiver android:name= ".Receiver.MyBroadcastREceiver2 ">< intent-filter android:priority= "1000" >< action android:name= "com.hy.mm.action.T "/>< /intent-filter>< /receiver>
// Transmit ordered broadcasting
Intent intent = new Intent("com.hy.mm.action.T");
intent.putExtra("age", 15);
sendOrderedBroadcast(intent, null);

 

Leave a Reply

Your email address will not be published. Required fields are marked *