BroadcastReceiver 广播的使用 (activity广播到fragment中,使用静态的没有成功,我就使用动态的 ,是可以的)

</pre><pre name="code" class="java"><pre name="code" class="java"> //接收广播
public static  class Receiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			String coupons = intent.getExtras().getString("coupons");
			Message   ms=new Message();
			Bundle  bundle=new Bundle();
			ms.what=600;
			bundle.putString("coupons",coupons);
			ms.setData(bundle);
			cHandler.sendMessage(ms); 


		}
	}

//发送广播
            Intent intent = new Intent();  //Itent就是我们要发送的内容
            intent.putExtra("data", "this is data from broadcast "+Calendar.getInstance().get(Calendar.SECOND));  
            intent.setAction(flag);   //设置你这个广播的action,只有和这个action一样的接受者才能接受者才能接收广播
            sendBroadcast(intent);   //发送广播

//注册广播(动态注册)
                receiver=new Receiver();
	        IntentFilter filter = new IntentFilter();
		filter.addAction("com.cardvalue.sys.receiver");    //只有持有相同的action的接受者才能接收此广播
		registerReceiver(receiver, filter);



//注册广播(静态注册)
<receiver android:name="net.youmi.android.AdReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <data android:scheme="package" />
            </intent-filter>
 </receiver>


静态和动态的区别
1.动态注册的广播永远要快于静态注册的广播,不管静态注册的优先级设置的多高,不管动态注册的优先级有多低>\
2.动态注册广播不是常驻型广播,也就是说广播跟随activity的生命周期。注意: 在activity结束前,移除广播接收器。
 静态注册是常驻型,也就是说当应用程序关闭后,如果有信息广播来,程序也会被系统调用自动运行。

<http://www.open-open.com/lib/view/open1342796818088.html   (可以参考)



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值