赞
踩
context.sendBroadcast(intent)
通常会伴随数据的传递intent.putExtra()
context.sendOrderBroadCast(intent)
这个在intent-filter中设置优先级android:priority
在Androidmanifest.xml中注册
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="com.example.broadcast" />
</intent-filter>
</receiver>
创建一个BroadCasteReceiver的子类的实例化对象
//1.创建一个对象
final MyReceiver myReceiver2 = new MyReceiver();
//2.设置button入口,设置事件监听
Button btn_register = findViewById(R.id.register_broadcast);
btn_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//添加一个Intentfilter对象,添加action属性
IntentFilter filter = new IntentFilter();
filter.addAction("MyReceiver2");
//将intentfilter与创建的子类对象关联起来
registerReceiver(myReceiver2,filter);
}
});
Button btn_unregister = findViewById(R.id.unregister_broadcast);
btn_unregister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
unregisterReceiver(myReceiver2);
}
});
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。