Android中BroadCast与Activity之间的通信
来源:互联网 发布:数据库刘卫国课后答案 编辑:程序博客网 时间:2024/05/29 15:53
在看本文之前,如果你对于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);
- }
- }
- <receiver android:name=".MyBroadCast">
- <intent-filter >
- <action android:name="com.gc.broadcase.test"/>
- </intent-filter>
- </receiver>
0 0
- Android中BroadCast与Activity之间的通信
- Android中BroadCast与Activity之间的通信
- Android中BroadCast与Activity之间的通信
- Android中Activity与Service之间的通信
- android中fragment与activity之间通信
- Android Service与Activity之间通信的几种方式(1)---通过broadcast(广播)的形式
- Android Service与Activity之间通信:通过Binder对象、Broadcast广播
- Android Service与Activity之间通信:通过Binder对象、Broadcast广播
- android activity与service之间的通信
- android Activity与Fragment之间的通信
- Android中Activity之间通信
- Android中Activity之间通信
- Android中Activity之间通信
- 【Android 应用开发】Activity生命周期 与 Activity 之间的通信
- service,broadcast与activity 通信
- Android中Activity与Service通信——Broadcast、Receiver、Intent、Bundle
- Android中Activity与Service通信——Broadcast、Receiver、Intent、Bundle
- android中Fragment与Activity之间通信原理以及例子
- 对EventLoop和性能问题处理的理解
- Types of Entity in Entity Framework
- Java并发编程:volatile关键字解析
- onClick、onLongClick事件同时触发
- 用c /c++编写mex 程序并编译MATLAB的Mex文件的方法
- Android中BroadCast与Activity之间的通信
- 递归删除文件或目录
- SpringMVC4+spring3+Myibats3
- 软件测试之-软件缺陷管理
- 信心等于成功
- ora-00844和ora-00851错误,大致是指SGA_MAX_SIZE设置过大
- BlockingQueue
- C++ primer 读书笔记(第5章)语句
- Mac OS 10.10 Sites 创建附带数据库