广播Broadcast简单使用

android广播最简单使用,在需要接收广播的界面里使用如下方法:

 

/**
	 * 注册广播
	 */
	private void registerBroadcast() {
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(Constant.BROADCAST_ACTION_DEPLOY_LINE);
		myBroadcastReciver = new MyBroadcastReciver();
		registerReceiver(myBroadcastReciver, intentFilter);
	}

	/**
	 * 自定义一个广播接收器
	 * 
	 * @date 2013-10-28 下午7:59:56
	 * @version V1.0
	 */
	private class MyBroadcastReciver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			
			/**
			 * 跳到从设置界面跳发过来的广播,就将这个界面结束掉,让用户到主页上去
			 */
			if (action.equals(Constant.BROADCAST_ACTION_DEPLOY_LINE)) {
				finish();
			}
		}
	}
	

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// 在结束时可取消广播
		if (myBroadcastReciver != null) {
			unregisterReceiver(myBroadcastReciver);
		}
	}
	

 

在onCreate方法里注册一下广播即可:

 /**
		 * 注册广播接
		 */
		registerBroadcast();

 

 

定义广播名:

/**
	 * 广播action名
	 */
	public final static String BROADCAST_ACTION_DEPLOY_LINE = "com.biao.deployline";

 

 

 

 

(2)在发送广播的地方,简单使用代码如下就可以发了:

/**
					 * 发送 一个无序广播
					 */
					Intent intent = new Intent();
					intent.setAction(Constant.BROADCAST_ACTION_DEPLOY_LINE);
					sendBroadcast(intent);

 

 

如我代码使用的:

private AsyncHttpResponseHandler depolylineHandler = new AsyncHttpResponseHandler(){
		@Override
    	public void onSuccess(String content) {
			try {
				if(ServerDataParseUtil.deployLine(content)){
					showToast("路线发布成功");
					
					
					/**
					 * 发送 一个无序广播
					 */
					Intent intent = new Intent();
					intent.setAction(Constant.BROADCAST_ACTION_DEPLOY_LINE);
					sendBroadcast(intent);
					
					
					finish();
				}
			} catch (JSONException e) {
				showToast("数据异常!");
			} catch (ResponseException e) {
				showToast(e.getErrorMessage());
			}
    	};
    	
    	public void onFailure(Throwable error, String content) {
    		CLog.d(TAG, "deploy line failed! content:" + content);
    	};
	};
	

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值