Android中BroadCast与Activity之间的通信

在看本文之前,如果你对于Android的广播机制不是很了解,建议先行阅读我转载的一篇博文:图解 Android 广播机制

由于本案例比较简单,故直接在此贴出代码,不做过多的阐述。

先上效果截图:

MainActivity的代码如下:
package com.gc.testbroadcasedemo;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
/**
 * 
 * @author Android将军
 *
 */
public class MainActivity extends Activity {
	private Button mButton;
	private TextView mTextView;
	public static String ACTION_INTENT_TEST = "com.gc.broadcase.test";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mTextView = (TextView) findViewById(R.id.message_tv);
		mButton = (Button) findViewById(R.id.send_btn);
		mButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent mIntent = new Intent(ACTION_INTENT_TEST);
				sendBroadcast(mIntent);
			}
		});
		registerMessageReceiver();
	}
	//在销毁时要与广播解绑
	@Override
	protected void onDestroy() {
		unregisterReceiver(mMessageReceiver);
		super.onDestroy();
	}

	public MessageReceiver mMessageReceiver;
	public static String ACTION_INTENT_RECEIVER = "com.gc.broadcast.receiver";

	/**
	 * 动态注册广播
	 */
	public void registerMessageReceiver() {
		mMessageReceiver = new MessageReceiver();
		IntentFilter filter = new IntentFilter();

		filter.addAction(ACTION_INTENT_RECEIVER);
		registerReceiver(mMessageReceiver, filter);
	}

	public class MessageReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			if (intent.getAction().equals(ACTION_INTENT_RECEIVER)) {
				mTextView.setText(intent.getStringExtra("message"));
			}
		}

	}
}

MyBroadCast的代码如下:
package com.gc.testbroadcasedemo;



import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
/**
 * 
 * @author Android将军
 *
 */
public class MyBroadCast extends BroadcastReceiver{

	public MyBroadCast()
	{
		Log.v("BROADCAST_TAG", "MyBroadCast");
	}
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		Log.v("BROADCAST_TAG", "onReceive");
		Bundle bundle = intent.getExtras();
		if(intent.getAction().equals(MainActivity.ACTION_INTENT_TEST))
		{
			processCustomMessage(context, bundle);
		}
	}
	//send msg to MainActivity
		private void processCustomMessage(Context context, Bundle bundle) {
			
				Intent mIntent=new Intent(MainActivity.ACTION_INTENT_RECEIVER);
				mIntent.putExtra("message", "测试Broadcast与Activity之间的通信");
				context.sendBroadcast(mIntent);
				
		}

}
案例工程目录如下:

在AndroidManifest中静态注册MyBroadCast广播,代码如下:
 <receiver android:name=".MyBroadCast">
            <intent-filter >
                <action android:name="com.gc.broadcase.test"/>
            </intent-filter>
        </receiver>
转载请注明出处: http://blog.csdn.net/android_jiangjun/article/details/39928243

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
西 安 邮 电 大 学 (计算机学院) 课内实验报告 实验名称: 界面设计:基本组件 专 业: 网络工程 班 级: 姓 名: 学 号: 指导教师: 日 期: 2017年4月20日 一.实验目的 1. 掌握常用组件在布局文件的设置 2. 掌握在java程序获取组件值 3. 掌握对组件值得验证 4. 掌握基本常用的监听器,和事件处理 5. 掌握将组件值提交到下一个Activity活动的方法 二.实验环境 JDK的版本: "1.8.0_40" IDE: eclipse 4.6.1 模拟器: 夜神模拟器 三.实验内容 1. 补充完成下例空缺处,完成注册界面、部门列表框、 单击确定检查 提交成功、接受界面 四.实验过程及分析 1. 设计UI界面 1. 编写布局代码,如下 <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical"> <!-- 主布局添加文本框和输入框 --> <TextView android:text = "@string/user" android:layout_width="match_parent" android:layout_height="wrap_content"/> <EditText android:id="@+id/name" android:layout_width="200sp" android:layout_height="wrap_content" android:hint="@string/inputuser"/> <!-- 在主布局添加文本框和密码框 --> <TextView android:text = "@string/password" android:layout_width="match_parent" android:layout_height="wrap_content"/> <EditText android:id="@+id/password" android:layout_width="200sp" android:layout_height="wrap_content" android:inputType="textPassword"/> <!-- 在主布局添加性别文本和复选框 --> <TextView android:text="@string/sex" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RadioGroup android:id="@+id/sex" android:orientation = "horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <!-- 设置默认选择的是女 --> <RadioButton android:id="@+id/man" android:text="@string/man" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RadioButton android:id="@+id/woman" android:checked="true" android:text="@string/woman" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RadioGroup> <!-- 在主布局添加联系电话文本框和输入框 --> <TextView android:text="@string/phone" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <EditText android:id="@+id/phone" android:inputType="text"phone" android:layout_width="200sp" android:layout_height="wrap_content"/> <!-- 在主布局添加部门文本框和列表框 --> <TextView an

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值