Android--An Example Analysis of Ordered Broadcasting--A Case of Intercepting Ordered Broadcasting

Keywords: Android xml encoding

Ordered broadcasting is a kind of synchronous broadcasting. Only one broadcasting receiver can receive this message at the same time after the broadcasting is sent. When the logic in the broadcasting receiver is finished, the broadcasting will continue to transmit. Therefore, the broadcast receiver at this time is sequential and can be intercepted.


The following is a case study:

1. Create a layout file

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:id="@+id/activity_main"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:paddingBottom="@dimen/activity_vertical_margin"  
    android:paddingLeft="@dimen/activity_horizontal_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingTop="@dimen/activity_vertical_margin"  
    android:background="@drawable/stitch_one"  
    tools:context="bzu.edu.cn.orderdbroadcast.MainActivity">  
  
  
    <Button  
        android:text="Sending Ordered Broadcasting"  
        android:onClick="send"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentTop="true"  
        android:layout_centerHorizontal="true"  
        android:layout_marginTop="61dp"  
        android:background="#FBC"  
        android:id="@+id/button" />  
</RelativeLayout>  


The layout file also defines a button button and registers an onclick click event send for the button. When the user clicks the button, an orderly broadcast will be sent.

2. Write MainActivity, code as follows:

package bzu.edu.cn.orderdbroadcast;  
  
import android.content.Intent;  
import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  
import android.view.View;  
  
public class MainActivity extends AppCompatActivity {  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
    }  
    public void send(View view){  
        Intent intent=new Intent();  
        //Define the event type of broadcast  
        intent.setAction("Intercept_Stitch");//Construct intent, set action. Send out custom broadcasts.  
        //Send Broadcasting  
        sendBroadcast(intent,null);//null is the permission of the broadcaster  
    }  
}  
In the above code, sendOrdered Broadcast (intent,null) is used to send an ordered broadcast. In this method, two parameters are accepted. The first parameter is the specified intention, setting the broadcast event bzu to be sent, and edu. cn. The second parameter specifies the recipient's permission. If you don't want all recipients to see it, you can display the permissions of the designated recipient.

3. Adding Broadcast Receiver

package bzu.edu.cn.orderdbroadcast;  
  
import android.content.BroadcastReceiver;  
import android.content.Context;  
import android.content.Intent;  
import android.util.Log;  
  
public class MyBroadcastReceiverOne extends BroadcastReceiver {  
    public MyBroadcastReceiverOne() {  
    }  
//Rewriting onReceive Method  
    @Override  
    public void onReceive(Context context, Intent intent) {  
        Log.i("MyBroadcastReceiverOne","Custom Broadcast Receiver One,Received broadcast events");  
    }  
}  


package bzu.edu.cn.orderdbroadcast;  
  
import android.content.BroadcastReceiver;  
import android.content.Context;  
import android.content.Intent;  
import android.util.Log;  
  
public class MyBroadcastReceivertwo extends BroadcastReceiver {  
    public MyBroadcastReceiverOne() {  
    }  
//Rewriting onReceive Method  
    @Override  
    public void onReceive(Context context, Intent intent) {  
        Log.i("MyBroadcastReceiverOne","Custom Broadcast Receiver two,Received broadcast events");  
    }  
}  

package bzu.edu.cn.orderdbroadcast;  
  
import android.content.BroadcastReceiver;  
import android.content.Context;  
import android.content.Intent;  
import android.util.Log;  
  
public class MyBroadcastReceiverthree extends BroadcastReceiver {  
    public MyBroadcastReceiverOne() {  
    }  
//Rewriting onReceive Method  
    @Override  
    public void onReceive(Context context, Intent intent) {  
        Log.i("MyBroadcastReceiverOne","Custom Broadcast Receiver three,Received broadcast events");  
    }  
}  


Add three broadcasting receivers, and different broadcasting receivers print different prompts.

4. Register in the manifest file and assign them different priorities. The code is as follows:

//Register receiver.  
        <receiver  
            android:name=".MyBroadcastReceiverOne"  
            android:enabled="true"  
            android:exported="true" >  
            <intent-filter  android:priority="1000"></intent-filter><!--set priority-->  
        </receiver>  
        <receiver  
            android:name=".MyBroadcastReceiverTwo"  
            android:enabled="true"  
            android:exported="true" >  
            <intent-filter  android:priority="800"></intent-filter>  
        </receiver>  
        <receiver  
            android:name=".MyBroadcastReceiverThree"  
            android:enabled="true"  
            android:exported="true">  
            <intent-filter  android:priority="900"></intent-filter>  
        </receiver>  
    </application>  
In the above code, Li Shiyong's Android priority specifies the priority of broadcasting.

The case is completed.



Posted by cpace1983 on Tue, 25 Jun 2019 15:44:05 -0700