http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 1861|回复: 3

[新手开发之旅] Android新手开发之旅-使用广播进行传值

[复制链接]
发表于 2018-12-31 14:39:24 | 显示全部楼层 |阅读模式
本帖最后由 liu 于 2018-12-29 15:46 编辑

用法:

要传值的类:
[Java] 纯文本查看 复制代码
Intent intent = new Intent();
                intent.setAction("自己定义");
                intent.putExtra("content", "要传的值");
                sendBroadcast(intent);

要接收值的类:
[Java] 纯文本查看 复制代码
 private void getBroadCast() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("要和传值时设置的action保持一致");
        registerReceiver(myBroadcastReceive, intentFilter);
    }

    BroadcastReceiver myBroadcastReceive = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String content = intent.getStringExtra("content");
             Log.i("TAG",intent.getStringExtra("content"));
        }
    };


注意:退出时注销广播

[Java] 纯文本查看 复制代码
 @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myBroadcastReceive);
    }





举个两个Activity之间使用广播传值的例子:
activity_main.xml:
[XML] 纯文本查看 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转" />
</LinearLayout>

activity_second.xml:
[XML] 纯文本查看 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:hint="输入内容" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="传值" />
</LinearLayout>

SecondActivity:
[Java] 纯文本查看 复制代码
public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        Button mButton = (Button) findViewById(R.id.btn);
        final EditText mEditText = (EditText) findViewById(R.id.et);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("自己定义");
                intent.putExtra("content", "要传的值");
                sendBroadcast(intent);
                finish();
            }
        });
    }

}


MainActivity:
[Java] 纯文本查看 复制代码
public class MainActivity extends AppCompatActivity {
    private TextView mTextView;
    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextView = (TextView) findViewById(R.id.tv);
        mButton = (Button) findViewById(R.id.btn);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });
        getBroadCast();
    }

    private void getBroadCast() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("要和传值时设置的action保持一致");
        registerReceiver(myBroadcastReceive, intentFilter);
    }

    BroadcastReceiver myBroadcastReceive = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String content = intent.getStringExtra("content");
            mTextView.setText("获取到的值-----" + content);
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myBroadcastReceive);
    }
}


运行效果:

         x.gif








1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2018-12-31 16:58:28 | 显示全部楼层
强烈支持楼主ing……
发表于 2018-12-31 19:25:42 | 显示全部楼层
我只是路过打酱油的。
发表于 2018-12-31 20:47:18 | 显示全部楼层
我只是路过打酱油的。
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-20 18:36

© 2014-2021

快速回复 返回顶部 返回列表