广播的基础

四大组件:

Activity

Content provider 内容提供者

Broadcast receiver 广播接受者

Service  服务

电台:   发送广播
收音机: 接受广播

android系统下的广播:

电池电量低。
电池充电完毕
短信到来了
程序安装卸载
sd卡卸载 安装


步骤L

1.写一个类继承广播接受者(BroadcastReceiver)
2.在清单文件配置关心的动作(就是你监听那个广播)
3.一旦广播事件发生了,就会执行广播接受者的onreceive方法

短信到来的广播接受者 4.4以后就不能用了

4.2


写一个文本数据:

/**
 * getSharedPreferences  用来存一般的文本数据 
 * 第一个参数为要设置的名字
 * 第一个参数为要设置的模式
 * */
sp =this. getSharedPreferences("config", MODE_PRIVATE);

Editor editor = sp.edit();
editor.putString("ipnumber", number);
editor.commit();


获取:

SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
setResultData(sp.getString("ipnumber", "") + number);



getResultData和setResultData只对有序广播有用


 

/**
	 * 发送无序广播
	 * */
	public void send1(View v){
		Intent intent = new Intent();
		intent.setAction("com.sqf.broadcasttest.songwennuan");
		intent.putExtra("msg", "发1万元");
		sendBroadcast(intent);//无序广播,不可以被拦截,不可终止
	}
	
	/**
	 * 发送有序广播
	 * */
	public void send2(View v){
		Intent intent = new Intent();
		intent.setAction("com.sqf.broadcasttest.songwennuan");
		
		//有序广播,可以被拦截,可终止,可以修改数据
		/**
		 * sendOrderedBroadcast参数
		 * 1。 intent
		 * 2.接受者权限   String naming a permissions that a receiver must hold
		 * 			  in order to receive your broadcast. If null,
		 * 			  no permission is required.
		 * 3.最终广播发给谁,这个广播不可用被拦截   Your own BroadcastReceiver to treat as the final receiver of the broadcast.
		 * 4.     A custom Handler with which to schedule the resultReceiver callback; if null it will be scheduled in the Context's main thread.
		   5.     An initial value for the result code. Often Activity.RESULT_OK.
		   6.要初始的数据
		   7.是否在intent中添加数据
		 * */
		sendOrderedBroadcast(intent, null , new finalReceiver(), null, 0,"要给农民兄弟发10000元", null);
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值