public class MyActivity extends Activity {
private MySelfReceiver1 receiver1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
//在界面注册一个广播,在配置文件注册两个接受者,设置优先级
//推荐使用
receiver1=new MySelfReceiver1();
IntentFilter filter=new IntentFilter("com.ruicaiedu.day1128_MySelf.MySelfReceiver1");
registerReceiver(receiver1, filter);
}
@Override
protected void onDestroy() {
unregisterReceiver(receiver1);
super.onDestroy();
}
//这只发送了广播,没有进行管理
public void bReceiver1(View v) {// 第一个按钮的点击方法
// 发送广播
// Intent intent = new Intent(MyActivity.this,
// MySelfReceiver1.class);//指定接收者
// sendOrderedBroadcast(intent, null);
//第三种,触发广播
Intent intent=new Intent("com.ruicaiedu.day1228_Myself.MySelfReceiver1");
sendOrderedBroadcast(intent, null);
}
public void bReceiver2(View v) {// 第二个按钮的点击方法
// 发送广播
// Intent intent = new Intent(MyActivity.this,
// MySelfReceiver2.class);
// sendOrderedBroadcast(intent, null);
//第三种,触发广播
Intent intent=new Intent("com.ruicaiedu.day1228_Myself.MySelfReceiver1");
sendOrderedBroadcast(intent, null);
}
}
配置文件如下:
<!--注册一个自定义光播 -->
<receiver android:name="com.ruicaiedu.day1228_Myself.MySelfReceiver1">接收者类名
<intent-filter >
<action android:name="com.ruicaiedu.day1228_Myself.MySelfReceiver1"/>光播类名
</intent-filter>
</receiver>
<!--注册一个自定义光播 -->
<receiver android:name="com.ruicaiedu.day1228_Myself.MySelfReceiver2">
<intent-filter android:priority="1000">
<action android:name="com.ruicaiedu.day1228_Myself.MySelfReceiver1"/>
</intent-filter>
</receiver>