public void click(View v){
Intent intent = new Intent();
intent.setAction("any.is.possible");
sendBroadcast(intent);
}
public void myReceiver extends BroadcastReceiver{
public void onReceive(Context ctx, Intent intent){
}
}
- 无序广播:同时收到,随机调用先后,系统不保证调用,sendBroadcast是无序广播,而且无法在广播里面修改数据
- 有序广播:接收有顺序,按接收顺序调用
public void click(View v){
Intent intent = new Intent();
intent.setAction("anything.is.possible");
sendOrderedBroadcast(intent, null, new FinalReceiver(), null, 0, "msg content", null);
}
class FinalRecevier extends BroadcastReceiver{
public void onReceive(Context c, Intent t){
}
}
- 接收者
<receiver>
<intent-filter priority="1000">
<action android:name="..."/>
</intent-filter>
</receiver>
public OneReciver extends ...{
public void onRecevie(...){
String txt = getResultData();
setResultData(txt);//设置广播数据,无序广播数据没用
abortBroadcast();// 广播丢弃,后续接收不到
}
}