android中的广播分有序广播和无序广播,无序广播比较容易理解,有序广播是根据你在清单中为每个reciver设置priority的大小(越大优先级越高)将消息依次的传递(由高到低),
既然有了顺序就可以实现消息的一级一级的拦击,或者在某一级中,修改传递来的数据,发送到下一级。(例子:车载中的发送消息,例如1号车(最高指挥车)下达消息给2号车,2号车接受到消息之后,根据命令将消息详细化发给3号车(不一定是一个量车,有的车优先级可以相同))。
拦截可以使用abortBroadCast来拦截。数据的设置如果是原始广播发送过来的,可以使用intent.getStringExtra("msg")获得原始数据,你可以将新的数据使用setResultExtras()封装传递到下一级去(下一级接收getResultExtras(true)),也可以使用setResultData()封装数据发送到下一级(下一级使用getResultData接收)
示例代码:
package com.example.demoorderbroadcast;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
private View btnSendOrderBroadCast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
btnSendOrderBroadCast = this.findViewById(R.id.btn);
btnSendOrderBroadCast.setOnClickListener(new MyOnclickListner());
}
private class MyOnclickListner implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(btnSendOrderBroadCast==v){
sendOrderBroadCast();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void sendOrderBroadCast() {
Intent intent=new Intent("com.pzf.mybroadcast");//清单文件中配置的
intent.putExtra("msg", "ni hao ");
sendOrderedBroadcast(intent, "com.pzf.permission");//receiverPermission:是自定义个权限
}
}
package com.example.demoorderbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class MyOrderBroadcastReciver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String strMsg = intent.getStringExtra("msg");
Log.e("pzf", "第一个:"+strMsg);
Bundle extras=new Bundle();
extras.putString("msg", "第一个界面传过来的"+strMsg);
setResultExtras(extras); //继续向下川
}
}
package com.example.demoorderbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class MyOrderBroadcastReciverTwo extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// abortBroadcast();//开启此处,可以截断广播,不让它传到third中
String strMsg = intent.getStringExtra("msg"); //获取广播的原始数据
Log.e("pzf", "第二个:"+strMsg);
Bundle extras=new Bundle();
extras.putString("msg", "第二个界面传过来的"+strMsg);
setResultExtras(extras); //继续向下川
setResultData("第二个:"+strMsg); // "第二个:"+strMsg这是two中新的数据,传递到third中,在third中是用getResultData来获得setResultData("")中的数据
}
}
package com.example.demoorderbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class MyOrderBroadcastReciverThird extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String resultData = getResultData();//获取two中setResultData中的数据
Bundle bundle = getResultExtras(true);//获取two中setResultExtras中的数据
String bundleData = bundle.getString("msg", "");//获取two中setResultExtras()中的数据
String strMsg = intent.getStringExtra("msg");//获取广播的原始数据
Log.e("pzf", "第三个:"+strMsg);
Log.e("pzf", "two中传到third新的数据:"+resultData);
Log.e("pzf", "two中setResultExtras(extras)传到third新的数据:"+bundleData);
}
}
源码地址:
http://download.csdn.net/detail/pangzaifei/6939891