有序广播的发送

发送端主函数

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void Onclick(View v){
        Intent intent = new Intent();
        intent.setAction("sendRice");
        sendOrderedBroadcast(intent,null,new FinalRiciver(),null,1,"发了1000斤大米",null);
    }
}

FinalRiciver

public class FinalRiciver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String resultData = getResultData();
        Toast.makeText(context,"最终"+resultData,Toast.LENGTH_LONG).show();
}
}

接收端函数

市长

public class CityRicever extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String resultData = getResultData();
        Toast.makeText(context,"市长"+resultData,Toast.LENGTH_LONG).show();
        setResultData("发了500斤大米");
    }
}

县长

public class ViligerRicever extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String resultData = getResultData();
        Toast.makeText(context,"村长"+resultData,Toast.LENGTH_LONG).show();
        setResultData("发了200斤大米");
    }
}

农民

    public void onReceive(Context context, Intent intent) {
        String resultData = getResultData();
        Toast.makeText(context,"农民"+resultData,Toast.LENGTH_LONG).show();
    }

manifest的配置

 
            
                

                
            
        

        
            
                
            
        
        
            
                
            

        
        
            
                
            
        

特殊广播

有序广播的发送_第1张图片
image.png

你可能感兴趣的:(有序广播的发送)