在代码中发送消息包裹,调用意图对象的putExtras方法,即可存入消息包裹。
在代码中接收消息包裹,调用意图对象的getExtras方法,即可取出消息包裹。
例:
- //创建一个包裹
- Bundle bundle = new Bundle();
- bundle.putString("request_content",tv_send.getText().toString());
- intent.putExtras(bundle);
Intent使用Bundle对象存放待传递的数据信息。
Bundle对象操作各类型数据的读写,方法说明如下:

发送页面的xml文件
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
-
- <TextView
- android:id="@+id/tv_send"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="这是要发送的文字"/>
-
- <Button
- android:id="@+id/btn_send"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:text="发送文字"/>
-
- </LinearLayout>
java类
- public class ActSendActivity extends AppCompatActivity implements View.OnClickListener {
-
- private TextView tv_send;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_act_send);
- tv_send = findViewById(R.id.tv_send);
- findViewById(R.id.btn_send).setOnClickListener(this);
- }
-
- @Override
- public void onClick(View view) {
- Intent intent = new Intent(this,ActReceiveActivity.class);
- //创建一个包裹
- Bundle bundle = new Bundle();
- bundle.putString("request_content",tv_send.getText().toString());
- intent.putExtras(bundle);
- startActivity(intent);
- }
- }
接收页面的xml文件
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
-
- <TextView
- android:id="@+id/tv_receive"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
-
- </LinearLayout>
java类
- public class ActReceiveActivity extends AppCompatActivity {
-
- private TextView v_receive;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_act_receive);
- v_receive = findViewById(R.id.tv_receive);
- //从上一个页面传来的意图中获取包裹
- Bundle bundle = getIntent().getExtras();
- String request_content = bundle.getString("request_content");
- String desc = String.format("收到请求消息:\n内容为:%s",request_content);
- v_receive.setText(desc);
- }
- }

处理下一个页面的应答数据,详细步骤说明如下:
例:
请求页面xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
-
- <TextView
- android:id="@+id/tv_request"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
-
- <Button
- android:id="@+id/btn_request"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:text="传送请求数据"/>
-
- <TextView
- android:id="@+id/tv_response"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
-
-
- </LinearLayout>
java类
- public class ActResquestActivity extends AppCompatActivity implements View.OnClickListener {
- private static final String mRequest = "i'm here";
- private ActivityResultLauncher<Intent> register;
- private TextView tv_response;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_act_resquest);
- TextView tv_request = findViewById(R.id.tv_request);
- tv_request.setText("待发送的信息:"+mRequest);
-
- tv_response = findViewById(R.id.tv_response);
-
- findViewById(R.id.btn_request).setOnClickListener(this);
-
- register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),result -> {
- if(result != null){
- Intent intent = result.getData();
- if(intent != null && result.getResultCode() == Activity.RESULT_OK){
- Bundle bundle = intent.getExtras();
- String response_content = bundle.getString("response_content");
- String desc = String.format("收到返回消息:%s",response_content);
- tv_response.setText(desc);
- }
- }
- });
- }
-
- @Override
- public void onClick(View view) {
- Intent intent =new Intent(this,ActResponseActivity.class);
- //创建一个包裹
- Bundle bundle = new Bundle();
- bundle.putString("request_content",mRequest);
- intent.putExtras(bundle);
- register.launch(intent);
- }
- }
应答页面xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
-
- <TextView
- android:id="@+id/tv_request"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
-
- <Button
- android:id="@+id/btn_response"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:text="返回应答数据"/>
-
- <TextView
- android:id="@+id/tv_response"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
-
- </LinearLayout>
java类
- public class ActResponseActivity extends AppCompatActivity implements View.OnClickListener {
- private static final String mresponse = "over";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_act_response);
-
- TextView tv_request = findViewById(R.id.tv_request);
- Bundle bundle = getIntent().getExtras();
- String request_content = bundle.getString("request_content");
- String desc = String.format("收到请求:%s",request_content);
- tv_request.setText(desc);
- findViewById(R.id.btn_response).setOnClickListener(this);
- TextView tv_response = findViewById(R.id.tv_response);
- tv_response.setText("待返回消息:"+mresponse);
- }
-
- @Override
- public void onClick(View view) {
- Intent intent = new Intent();
- Bundle bundle = new Bundle();
- bundle.putString("response_content",mresponse);
- intent.putExtras(bundle);
- //携带意图返回上一页面,RESULT_OK表示处理成功
- setResult(Activity.RESULT_OK,intent);
- //结束页面
- finish();
- }
- }

