Android development: ordered broadcasting and unordered broadcasting

Disorderly broadcast image explanation: As long as I send it out on time, it is up to you to receive it or not.

 

Create a new app for sending broadcasts:

    public void click(View v) {
        Intent intent = new Intent();

        intent.setAction("org.dreamtech.custom");

        intent.putExtra( "name", "Out-of-order broadcast" );

        sendBroadcast (intent);
    }

 

 

Create a new app to receive broadcasts:

package org.dreamtech.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class ReceiveReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        String content = intent.getStringExtra("name");

        Toast.makeText(context, content, Toast.LENGTH_LONG).show();

    }

}

 

The second app pays attention to the configuration:

        <receiver android:name="org.dreamtech.receiver.ReceiveReceiver">
            <intent-filter >
                <action android:name="org.dreamtech.custom"/>
            </intent-filter>
        </receiver>

 

 

 

Deploy two apps:

Open the first app and click send, and a sentence will pop up: "unordered broadcast"

 

Image explanation of orderly broadcast: The general manager of a large company makes a decision and distributes it to the managers of the lower departments, and they explain it to their employees after processing

 

Create a new app to send ordered broadcasts:

    public void click(View v) {
        Intent intent = new Intent();
        intent.setAction("org.dreamtech.send");
        sendOrderedBroadcast(intent, null , new FinalReceiver(), null , 1 ,
                 "Ordered Broadcast", null );
    }
package org.dreamtech.demo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class FinalReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String content = getResultData();

        Toast.makeText(context, "Broadcast final result" + content, Toast.LENGTH_LONG).show();
    }

}

 

Create a new app to receive:

Level 4 reception:

package org.dreamtech.demo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class FirstReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String content = getResultData();

        Toast.makeText(context, "first level" + content, Toast.LENGTH_LONG).show();

        setResultData( "The first level has been processed" );
    }

}
package org.dreamtech.demo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class SecondReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String content = getResultData();

        Toast.makeText(context, "second level" + content, Toast.LENGTH_LONG).show();

        setResultData( "The second level has been processed" );
    }

}
package org.dreamtech.demo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class ThirdReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String content = getResultData();

        Toast.makeText(context, "third level" + content, Toast.LENGTH_LONG).show();

        setResultData( "The third level has been processed" );
        
        // terminate
         // abortBroadcast();
        
    }

}
package org.dreamtech.demo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class FourthReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String content = getResultData();

        Toast.makeText(context, "第四级" + content, Toast.LENGTH_LONG).show();
        
    }

}

 

Attention placement:

        <receiver android:name="org.dreamtech.demo.FirstReceiver" >
            <intent-filter android:priority="1000" >
                <action android:name="org.dreamtech.send" />
            </intent-filter>
        </receiver>
        <receiver android:name="org.dreamtech.demo.SecondReceiver" >
            <intent-filter android:priority="500" >
                <action android:name="org.dreamtech.send" />
            </intent-filter>
        </receiver>
        <receiver android:name="org.dreamtech.demo.ThirdReceiver" >
            <intent-filter android:priority="100" >
                <action android:name="org.dreamtech.send" />
            </intent-filter>
        </receiver>
        <receiver android:name="org.dreamtech.demo.FourthReceiver" >
            <intent-filter android:priority="50" >
                <action android:name="org.dreamtech.send" />
            </intent-filter>
        </receiver>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324694660&siteId=291194637