调用意图对象的putExtras方法,即可存入消息包裹。示例如下:
// 创建一个意图对象,准备跳到指定的活动页面 Intent intent = new Intent(this, ActReceiveActivity.class);
Bundle bundle = new Bundle(); // 创建一个新包裹
// 往包裹存入名叫request_time的字符串 bundle.putString("request_time", DateUtil.getNowTime());
// 往包裹存入名叫request_content的字符串 bundle.putString("request_content", tv_send.getText().toString());
intent.putExtras(bundle); // 把快递包裹塞给意图
startActivity(intent); // 跳转到意图指定的活动页面
调用意图对象的getExtras方法,即可取出消息包裹。示例如下:
// 从布局文件中获取名叫tv_receive的文本视图TextView tv_receive = findViewById(R.id.tv_receive);
// 从上一个页面传来的意图中获取快递包裹Bundle bundle = getIntent().getExtras();
// 从包裹中取出名叫request_time的字符串String request_time = bundle.getString("request_time");
// 从包裹中取出名叫request_content的字符串String request_content = bundle.getString("request_content");
String desc = String.format("收到请求消息:\n请求时间为%s\n请求内容为%s",request_time, request_content);
tv_receive.setText(desc); // 把请求消息的详情显示在文本视图上
===========================================================================================
第一个页面:
<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:padding="5dp"
android:text="今天的天气真不错"
android:textColor="#000000"
android:textSize="17sp" />
<Button
android:id="@+id/btn_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="发送以上文字"
android:textColor="#000000"
android:textSize="17sp" />
</LinearLayout>
package com.example.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
private TextView tv_send; // 声明一个文本视图对象
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 从布局文件中获取名叫tv_send的文本视图
tv_send = findViewById(R.id.tv_send);
findViewById(R.id.btn_send).setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if (v.getId() == R.id.btn_send)
{
// 创建一个意图对象,准备跳到指定的活动页面
Intent intent = new Intent(this, ActReceiveActivity.class);
Bundle bundle = new Bundle(); // 创建一个新包裹
// 往包裹存入名叫request_time的字符串
bundle.putString("request_time", DateUtil.getNowTime());
// 往包裹存入名叫request_content的字符串
bundle.putString("request_content", tv_send.getText().toString());
intent.putExtras(bundle); // 把快递包裹塞给意图
startActivity(intent); // 跳转到意图指定的活动页面
}
}
}
第二个页面;
<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"
android:padding="5dp"
android:textColor="#000000"
android:textSize="17sp" />
<Button
android:id="@+id/btn_receive"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="知道了"
android:textColor="#000000"
android:textSize="17sp" />
</LinearLayout>
package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class ActReceiveActivity extends AppCompatActivity implements View.OnClickListener
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_act_receive);
// 从布局文件中获取名叫tv_receive的文本视图
TextView tv_receive = findViewById(R.id.tv_receive);
findViewById(R.id.btn_receive).setOnClickListener(this);
// 从上一个页面传来的意图中获取快递包裹
Bundle bundle = getIntent().getExtras();
// 从包裹中取出名叫request_time的字符串
String request_time = bundle.getString("request_time");
// 从包裹中取出名叫request_content的字符串
String request_content = bundle.getString("request_content");
String desc = String.format("收到请求消息:\n请求时间为%s\n请求内容为%s", request_time, request_content);
tv_receive.setText(desc); // 把请求消息的详情显示在文本视图上
}
@Override
public void onClick(View v)
{
if (v.getId() == R.id.btn_receive)
{
finish(); // 结束当前的活动页面
}
}
}
时间工具类:
DateUtil
package com.example.myapplication;
import android.annotation.SuppressLint;
import java.text.SimpleDateFormat;
import java.util.Date;
@SuppressLint("SimpleDateFormat")
public class DateUtil {
// 获取当前的日期时间
public static String getNowDateTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
return sdf.format(new Date());
}
// 获取当前的时间
public static String getNowTime() {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
return sdf.format(new Date());
}
// 获取当前的时间(精确到毫秒)
public static String getNowTimeDetail() {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
return sdf.format(new Date());
}
}
结果:
===================================================================================
=========================================================================================