The Use of android Broadcasting

Keywords: Mobile Android less xml

Use of # android broadcasting

Before the broadcast used less, one time forgot how to write, today wrote down the convenience of their own use in the future when easy to consult.
There are two ways to use broadcasting in android.
Either way, you start by writing a Broadcast Receiver for a class inheritance system and rewriting the onReceive method

public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
    //Here intent can retrieve the incoming data when broadcasting
        Log.e("main", "23333");
    }
}


Then there are two ways to use it.
Mode 1: Static registered broadcasting (registered in AndroidMainfest.xml) is the best way to cancel broadcasting without having to manually register it.

<receiver android:name=".activity.MyBroadcastReceiver">
            <intent-filter>
                <action android:name="com.my.sentborad" />
            </intent-filter>
</receiver>


Then you can use it!
eg can send broadcasts in this way

 //Transmit broadcast
 bt_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setAction("sent");
                sendBroadcast(intent);
            }
        });


At this time, you can see the log in the onReciver method in the log!

Mode 2 Dynamic Registered Broadcasting This registered broadcasting requires manual cancellation of broadcasting
eg
Registered broadcasting

        br = new MyBroadcastReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.my.sentborad");
        registerReceiver(br, filter);
 //Transmit broadcast
 bt_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setAction("sent");
                sendBroadcast(intent);
            }
        });

Manual cancellation of broadcasting

 @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(br);
    }

Journal

 
**

Attention should be paid to uuuuuuuuuuuuu

** Actions must be added when broadcasting is registered, such as "com.my.sentborad" here.
In the beginning, it was the Internet plus action that couldn't receive broadcasts at all! uuuuuuuuuu Be careful here! Be careful here! Be careful here! Important things are to be repeated for 3 times!!!

Posted by Highlander on Fri, 25 Jan 2019 04:09:15 -0800