android利用广播在activity间传值

这篇博客详细介绍了如何在Android应用中利用广播机制,实现不同Activity之间的数据通信。首先展示了第一个布局,然后在第二个Activity中说明如何发送广播,并将值传递回第一个Activity。最后,提到了第二个布局的设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import static com.example.administrator.myandroid.BroadcastReceive.AnotherActivity.ACTION;

public class MyReceiveActivity extends Activity implements View.OnClickListener {

    private Button mBtnStartReceive;
    private Button mBtnStopReceive;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive);
        mBtnStartReceive = (Button) findViewById(R.id.start_receive);
        mBtnStopReceive = (Button) findViewById(R.id.stop_receive);

        mBtnStartReceive.setOnClickListener(this);
        mBtnStopReceive.setOnClickListener(this);

    }

    private boolean flagReceiveRegis;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start_receive:
                IntentFilter intentFilter = new IntentFilter();
                intentFilter.addAction(ACTION);  //这个ACTION和后面activity的ACTION一样就行,要不然收不到的
                registerReceiver(myBroadcastReceive, intentFilter);
                Log.i("广播", "----注册广播");
                flagReceiveRegis = true;
                startActivity(new Intent(this, AnotherActivity.class));

                break;

            case R.id.stop_receive:

                if (flagReceiveRegis) {
                    unregisterReceiver(myBroadcastReceive);
                    flagReceiveRegis = false;
                    Log.i("广播", "----注销了");
                } else {
                    Log.i("广播","----还没有开启广播");
                }
                break;
        }
    }
    BroadcastReceiver myBroadcastReceive = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("广播", "----接收到的是----" + intent.getStringExtra("msg"));
        }
    };

    @Override
    protected void onDestroy() {
        if (flagReceiveRegis) {
            unregisterReceiver(myBroadcastReceive);
            flagReceiveRegis = false;
            Log.i("广播", "----注销了");
        } else {
            Log.i("广播","----还没有开启广播");
        }
        super.onDestroy();

}

//第一个布局

<?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:orientation="vertical">

    <Button
        android:id="@+id/start_receive"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开启广播"/>

    <Button
        android:id="@+id/stop_receive"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="关闭注销广播"/>

</LinearLayout>


//第二个activity,,,将值通过广播传给第一个activity

public class AnotherActivity extends Activity implements View.OnClickListener {

    private Button mBtnSend;
    public static final String ACTION ="com.example.action";

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

        mBtnSend = (Button) findViewById(R.id.btn_send);
        mBtnSend.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_send:
                Intent intent = new Intent(ACTION);
                intent.putExtra("msg", "--我是发送给第一个界面的信息");
                sendBroadcast(intent);
                Log.i("广播","--发送了消息");
                finish();
                break;
        }
    }

//第二个布局

<?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:orientation="vertical">

    <Button
        android:id="@+id/btn_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送"/>

</LinearLayout>
//千万不要忘了在manifest中配置activity哦!!!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值