Activity向广播接收器传递数据很简单,只需要在发送广播前将数据put进Intent中就行了。

  广播接收器怎么向Activity传送数据?这里要用到接口,通过在广播接收器里定义一个接口,然后让接收广播接收器数据的Activity实现这个接口。先看下面的栗子,Activity发送一个广播,然后广播接收器返回一个字符串。

Activity布局文件

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.nangch.broadcastreceivertest.MainActivity"> <TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello" /> <Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送广播"/>
</LinearLayout>

Activity代码

 import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends AppCompatActivity implements MyReceiver.Message{ TextView tv;
MyReceiver myReceiver; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //注册广播接收器
myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.nangch.broadcasereceiver.MYRECEIVER");
registerReceiver(myReceiver, intentFilter); //因为这里需要注入Message,所以不能在AndroidManifest文件中静态注册广播接收器
myReceiver.setMessage(this); tv = (TextView) findViewById(R.id.tv);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.nangch.broadcasereceiver.MYRECEIVER");
intent.putExtra("hello", tv.getText()); //向广播接收器传递数据
sendBroadcast(intent); //发送广播
}
});
} @Override
public void getMsg(String str) {
//通过实现MyReceiver.Message接口可以在这里对MyReceiver中的数据进行处理
tv.append(str);
} @Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myReceiver); //注销广播接收器
}
}

广播接收器代码

 import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast; public class MyReceiver extends BroadcastReceiver {
private Message message; @Override
public void onReceive(Context context, Intent intent) {
//接收MainActivity传过来的数据
Toast.makeText(context, intent.getStringExtra("hello"), Toast.LENGTH_SHORT).show(); //调用Message接口的方法
message.getMsg(" world");
} interface Message {
public void getMsg(String str);
} public void setMessage(Message message) {
this.message = message;
}
}

效果图如下:

点击发送广播按钮后:

在MyReceiver中定义一个Message接口,并声明一个Message类型的成员变量message。然后让MainActivity实现这个接口,并调用setMessage方法将MainActivity注入,这样MainActivity实例就成了Myreceiver的成员变量message,这样就能处理MyReceiver中的数据了。这种思想和我们学习Android时设置按钮监听器的思想差不多,仔细想一下还是很好理解的。

演示实例源码下载

Android广播接收器和Activity间传递数据的更多相关文章

  1. Android学习手记(3) Activity间传递数据

    1. 简单数据传递 建立两个Activity,名称分别为MainActivity和TheAty,在MainActivity中新建一个Button,id为btnStartAty.在TheAty中新建一个 ...

  2. 使用Bundle在Activity间传递数据

    使用Bundle在Activity间传递数据 源Activity public class SourceActivty extends Activity { private Intent intent ...

  3. 第一课android开发之在activity间传递参数

    一.活动间简单参数传递:1.在布局中添加按钮,用<Button,用id设置id名称,id="@+id/这儿填写你要设置成的名称":用text设置按钮上显示的文字.text=& ...

  4. Android 笔记-Fragment 与 Activity之间传递数据

    Fragment 与 Activity之间传递数据有两种方法.一种是使用setArgument,一种是使用接口回调.以下先学习第一种方法. (1)使用setArgument方法: 为了便于理解,我在这 ...

  5. [android] 在不同的activity之间传递数据

    新建一个activity,继承Activity 清单文件中进行配置,添加<activity/>节点 设置名称 android:name=”.类名” 点 代表的是当前包名,也可以不写 新建一 ...

  6. Activity间传递数据

     1.从当前的Activity传递数据到下一个Activity: (1)发送方(当前的Activity): Bundle bundle = new Bundle(); bundle.putString ...

  7. Android程序中Acticity间传递数据

    在Android开发过程中,在不同的Acitivity之间传递数据的情况是非常常见的.我花费了一点时间来总结Acitivity之间的数据传递,记录下来. 1.简单传递键值对 这种传递方式非常简单,只需 ...

  8. 【Android 复习】 : Activity之间传递数据的几种方式

    在Android开发中,我们通常需要在不同的Activity之间传递数据,下面我们就来总结一下在Activity之间数据传递的几种方式. 1. 使用Intent来传递数据 Intent表示意图,很多时 ...

  9. Activity之间传递数据或数据包Bundle,传递对象,对象序列化,对象实现Parcelable接口

    package com.gaojinhua.android.activitymsg; import android.content.Intent; import android.os.Bundle; ...

随机推荐

  1. linux 使用文件作为交换分区

    sudo fallocate -l 4G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile ...

  2. js全选反选

    <style type="text/css"> table { width: 800px; text-align: left; border-collapse: col ...

  3. 更高效的MergeSort--稍微优化

    0. 简介 本文简要介绍一下比传统MergeSort更高效的算法,在原来的算法Merge基础上,少发生一半拷贝.欢迎探讨,感谢阅读. 原文链接如下:http://loverszhaokai.com/p ...

  4. 转载的 Linux下chkconfig命令详解

    Linux下chkconfig命令详解 chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. ...

  5. python 文件写入与储存

    python使用open()这个函数来打开文件返回对象: open 第二参数 "r" 以只读方式打开文件 "w" 以写入方法打开文件,会覆盖已储存的内容 &qu ...

  6. sql when null 判断

    Sql Server 中使用case when then 判断某字段是否为null,和判断是否为字符或数字时的写法不一样,如果不注意,很容易搞错 错误方法: CASE columnName WHEN ...

  7. AdaBoost学习笔记

    学习了李航<统计学习方法>第八章的提升方法,现在对常用的一种提升方法AdaBoost作一个小小的笔记,并用python实现书本上的例子,加深印象.提升方法(boosting)是一种常用的统 ...

  8. python 命令执行文件传递参数

    import os,sys for root,dirs,files in os.walk(sys.argv[1]): for name in files: print(os.path.join(roo ...

  9. MyBatis学习笔记(七)——Mybatis缓存

    转自孤傲苍狼的博客:http://www.cnblogs.com/xdp-gacl/p/4270403.html 一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓 ...

  10. mysql删除有外链索引数据,Cannot delete or update a parent row: a foreign key constraint fails 问题的解决办法

    mysql删除有外链索引数据Cannot delete or update a parent row: a foreign key constraint fails 问题的解决办法查询:DELETE ...