Android Intent意图(二):Bundle传递数据

一、Bundle的使用:

Bundle经常使用在Activity之间或者线程间传递数据,传递的数据可以是boolean、
byte、int、long、float、double、string等基本类型或它们对应的数组,也可以是对象或对象数组。

当Bundle传递的是对象或对象数组时,必须实现Serializable或Parcelable接口。
Bundle提供了各种常用类型的putXxx()/getXxx()方法,用于读写基本类型的数据。

 

二、Bundle和Intent区别:
Bundle只是一个信息的载体,内部其实就是维护了一个Map<String,Object>。
Intent负责Activity之间的交互,内部是持有一个Bundle的。

 

三、goal:

1.1、在activity之间传递信息,Bundle。

1.2、在线程之间使用传递信息,Bundle、Handler、Looper。

 

activity_main.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">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn1"
        android:textSize="32sp" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn2"
        android:textSize="32sp" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="绘梨衣"
        android:textSize="64sp" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn3"
        android:textSize="32sp" />

    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="绘梨衣"
        android:textSize="64sp" />

    <Button
        android:id="@+id/btn4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn4"
        android:textSize="32sp" />

</LinearLayout>

 MainActivity.java

package com.gatsby.intentbundlesend;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button btn1, btn2, btn3, btn4;
    TextView tv2, tv3;

    private Handler mHandler = null;
    private final int MSG_CRUSHGIRL = 0;
    private final int MSG_GATSBY = 1;

    class CrushHandler extends Handler {

        public CrushHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case MSG_CRUSHGIRL:
                    boolean crush_enable = (boolean) msg.obj;
                    if (crush_enable) {
                        tv2.setText("Crush 绘梨衣!");
                    }
                    break;
                case MSG_GATSBY:
                    //Bundle 读取数据
                    /*Bundle bundle = msg.getData();
                    String gatsby = bundle.getString("ageString");*/
                    String gatsby = msg.getData().getString("ageString");
                    tv3.setText("song:->" + gatsby);
                    break;
            }

        }
    }

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

        initView();
        //Handler初始化 需要注意, Handler初始化传入Looper对象是子线程中缓存的Looper对象
        mHandler = new CrushHandler(Looper.getMainLooper());
    }

    public void initView() {

        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);

        tv2 = (TextView) findViewById(R.id.tv2);
        tv3 = (TextView) findViewById(R.id.tv3);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn1:
                //在activity间传递信息
                Bundle bundle1 = new Bundle();        //得到bundle对象
                //Intent intent=new Intent(MainActivity.this,SecondActivity.class);
                Intent intent1 = new Intent();
                intent1.setClassName("com.gatsby.intentbundlesend",
                        "com.gatsby.intentbundlesend.SecondActivity");
                bundle1.putString("StringName", "绘梨衣");
                bundle1.putInt("intNumber", 180);
                intent1.putExtras(bundle1);            //通过intent将bundle传到另个Activity
                startActivity(intent1);
                break;
            case R.id.btn2:
                //Handle、Looper线程间传递
                //通过Handler将带有bundle数据的message放入消息队列,其他线程就可以从队列中得到数据
                boolean enable = true;
                Message msg1 = mHandler.obtainMessage(MSG_CRUSHGIRL);
                msg1.obj = enable;
                mHandler.sendMessage(msg1);
                break;
            case R.id.btn3:
                //线程间传递 bundle
                Message msg2 = mHandler.obtainMessage(MSG_GATSBY);
                Bundle bundle = new Bundle();
                bundle.putString("ageString", "This is age!");
                msg2.setData(bundle);
                mHandler.sendMessage(msg2);
                break;

        }
    }


}

 activity_second.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/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="绘梨衣"
        android:textSize="64sp" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="小怪兽"
        android:textSize="64sp" />

    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我爱绘梨衣"
        android:textSize="64sp" />


</LinearLayout>

 SecondActivity.java

package com.gatsby.intentbundlesend;

import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class SecondActivity extends AppCompatActivity {

    TextView tv1, tv2, tv3;

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

        initView();
        showTextView1();
    }

    public void initView() {
        tv1 = (TextView) findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);
        tv3 = (TextView) findViewById(R.id.tv3);
    }

    public void showTextView1() {
        Bundle bundle = getIntent().getExtras();
        String name = bundle.getString("StringName");
        int number = bundle.getInt("intNumber");
        tv1.setText("" + name + " " + number);
    }

}

 

posted @ 2020-05-22 17:10  CrushGirl  阅读(398)  评论(0编辑  收藏  举报