public class MainActivity extends Activity {
// 广播接收器的注册:1、动态注册 (程序) 2、静态注册( xml)
// 发送广播:1、普通广播 :如果有多个接收者,他们同时会受到广播,而且不能截断 2、有序广播:按照优先级的顺序收到广播,而且可以截断
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button but_send = (Button) findViewById(R.id.button1);
but_send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent it = new Intent();
it.setAction("com.cui.test001.sendBroadcast");
it.putExtra("name", "张三");
// 发送普通广播
// sendBroadcast(it);
// 发送有序广播
sendOrderedBroadcast(it, null);
}
});
}
(静态接收,更改配置xml
<receiver android:name="com.cui.test002.MyReceiver001" > //接收位置
<intent-filter android:priority="1" > // 优先级
<action android:name="com.cui.test001.sendBroadcast" /> //发送方
</intent-filter>
</receiver>
)
(
public class Receiver002 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String name = intent.getStringExtra("name");
Toast.makeText(context, "alalalal接收到啦!--" + name, Toast.LENGTH_SHORT)
.show();
// 截断广播
abortBroadcast();
}
}
)