首先复习下,页面跳转:
if (v.getId() == R.id.btn_next)
{
// startActivity(new Intent(this, ActSendActivity.class)); //创建一个目标确定的意图
Intent intent = new Intent(); //创建一个新意图
intent.setClass(this,ActSendActivity.class); //设置要跳转的目标活动
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //设置启动标志
startActivity(intent); //跳转到意图指定的活动页面
}



调用意图对象的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" >
<Button
android:id="@+id/btn_next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="点击跳转下一个activity页面"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>

第一个页面代码:
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_next).setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if (v.getId() == R.id.btn_next)
{
// startActivity(new Intent(this, ActSendActivity.class)); //创建一个目标确定的意图
Intent intent = new Intent(); //创建一个新意图
intent.setClass(this,ActSendActivity.class); //设置要跳转的目标活动
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //设置启动标志
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_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 androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
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); // 从布局文件中获取名叫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); // 把快递包裹塞给意图
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //设置启动标志
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 androidx.appcompat.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());
}
}




=============================================================================================

处理下一个页面的应答数据,详细步骤说明如下:
(1)上一个页面打包好请求数据,调用startActivityForResult方法执行跳转动作
(2)下一个页面接收并解析请求数据,进行相应处理
(3)下一个页面在返回上一个页面时,打包应答数据并调用setResult方法返回数据包裹
(4)上一个页面重写方法onActivityResult,解析获得下一个页面的返回数据
第一个页面布局:
<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_next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="点击跳转下一个activity页面"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>

第一个页面代码:
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_next).setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if (v.getId() == R.id.btn_next)
{
// startActivity(new Intent(this, ActSendActivity.class)); //创建一个目标确定的意图
Intent intent = new Intent(); //创建一个新意图
intent.setClass(this,ActRequestActivity.class); //设置要跳转的目标活动
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //设置启动标志
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_request"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:textColor="#000000"
android:textSize="17sp" />
<Button
android:id="@+id/btn_request"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="传送请求数据"
android:textColor="#000000"
android:textSize="17sp" />
<TextView
android:id="@+id/tv_response"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:textColor="#000000"
android:textSize="17sp" />
</LinearLayout>

第二个页面代码:
package com.example.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class ActRequestActivity extends AppCompatActivity implements View.OnClickListener
{
private String mRrequest = "你吃饭了吗?来我家吃吧";
private TextView tv_response; // 声明一个文本视图对象
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_act_request);
// 从布局文件中获取名叫tv_request的文本视图
TextView tv_request = findViewById(R.id.tv_request);
tv_request.setText("待发送的消息为:"+mRrequest);
// 从布局文件中获取名叫tv_response的文本视图
tv_response = findViewById(R.id.tv_response);
findViewById(R.id.btn_request).setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if (v.getId() == R.id.btn_request)
{
// 创建一个意图对象,准备跳到指定的活动页面
Intent intent = new Intent(this, ActResponseActivity.class);
Bundle bundle = new Bundle(); // 创建一个新包裹
// 往包裹存入名叫request_time的字符串
bundle.putString("request_time", DateUtil.getNowTime());
// 往包裹存入名叫request_content的字符串
bundle.putString("request_content", mRrequest);
intent.putExtras(bundle); // 把快递包裹塞给意图
// 期望接收下个页面的返回数据。第二个参数为本次请求代码
startActivityForResult(intent, 0);
}
}
// 从下一个页面携带参数返回当前页面时触发。其中requestCode为请求代码,
// resultCode为结果代码,intent为下一个页面返回的意图对象
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) // 接收返回数据
{
super.onActivityResult(requestCode, resultCode, intent);
// 意图非空,且请求代码为之前传的0,结果代码也为成功
if (intent!=null && requestCode==0 && resultCode== Activity.RESULT_OK)
{
Bundle bundle = intent.getExtras(); // 从返回的意图中获取快递包裹
// 从包裹中取出名叫response_time的字符串
String response_time = bundle.getString("response_time");
// 从包裹中取出名叫response_content的字符串
String response_content = bundle.getString("response_content");
String desc = String.format("收到返回消息:\n应答时间为:%s\n应答内容为:%s", response_time, response_content);
tv_response.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_request"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:textColor="#000000"
android:textSize="17sp" />
<Button
android:id="@+id/btn_response"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="返回应答数据"
android:textColor="#000000"
android:textSize="17sp" />
<TextView
android:id="@+id/tv_response"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:textColor="#000000"
android:textSize="17sp" />
</LinearLayout>

第三个页面代码:
package com.example.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class ActResponseActivity extends AppCompatActivity implements View.OnClickListener
{
private String mResponse = "我吃过了,还是你来我家吃";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_act_response);
// 从布局文件中获取名叫tv_request的文本视图
TextView tv_request = findViewById(R.id.tv_request);
findViewById(R.id.btn_response).setOnClickListener(this);
// 从布局文件中获取名叫tv_response的文本视图
TextView tv_response = findViewById(R.id.tv_response);
tv_response.setText("待返回的消息为:"+mResponse);
// 从上一个页面传来的意图中获取快递包裹
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_request.setText(desc); // 把请求消息的详情显示在文本视图上
}
@Override
public void onClick(View v)
{
if (v.getId() == R.id.btn_response)
{
Intent intent = new Intent(); // 创建一个新意图
Bundle bundle = new Bundle(); // 创建一个新包裹
// 往包裹存入名叫response_time的字符串
bundle.putString("response_time", DateUtil.getNowTime());
// 往包裹存入名叫response_content的字符串
bundle.putString("response_content", mResponse);
intent.putExtras(bundle); // 把快递包裹塞给意图
// 携带意图返回上一个页面。RESULT_OK表示处理成功
setResult(Activity.RESULT_OK, intent);
finish(); // 结束当前的活动页面
}
}
}






====================================================================================

Intent是各个组件之间信息沟通的桥梁,它用于Android各组件之间的通信,主要完成下列工作:
(1)标明本次通信请求从哪里来、到哪里去、要怎么走。
(2)发起方携带本次通信需要的数据内容,接收方从收到的意图中解析数据。
(3)发起方若想判断接收方的处理结果,意图就要负责让接收方传回应答的数据内容。


显式Intent,直接指定来源活动与目标活动,属于精确匹配。它有三种构建方式:
(1)在Intent的构造函数中指定,示例代码如下:
Intent intent = new Intent(this, ActNextActivity.class); // 创建一个目标确定的意图
(2)调用意图对象的setClass方法指定,示例代码如下:
Intent intent = new Intent(); // 创建一个新意图intent.setClass(this, ActNextActivity.class); // 设置意图要跳转的目标活动
(3)调用意图对象的setComponent方法指定,示例代码如下:
Intent intent = new Intent(); // 创建一个新意图ComponentName component = new ComponentName(this, ActNextActivity.class); // 创建包含目标活动在内的组件名称对象intent.setComponent(component); // 设置意图携带的组件信息
PS: 也可以跳转下个页面
@Override
public void onClick(View v)
{
if (v.getId() == R.id.btn_next)
{
// startActivity(new Intent(this, ActSendActivity.class)); //创建一个目标确定的意图
// Intent intent = new Intent(); //创建一个新意图
//
// intent.setClass(this,ActRequestActivity.class); //设置要跳转的目标活动
//
// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //设置启动标志
//
// startActivity(intent); //跳转到意图指定的活动页面
Intent intent = new Intent(); // 创建一个新意图
ComponentName component = new ComponentName(this, ActRequestActivity.class); // 创建包含目标活动在内的组件名称对象
intent.setComponent(component); // 设置意图携带的组件信息
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" >
<Button
android:id="@+id/btn_next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="点击跳转下一个activity页面"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>

第一个页面代码:
package com.example.myapplication;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_next).setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if (v.getId() == R.id.btn_next)
{
// startActivity(new Intent(this, ActSendActivity.class)); //创建一个目标确定的意图
// Intent intent = new Intent(); //创建一个新意图
//
// intent.setClass(this,ActRequestActivity.class); //设置要跳转的目标活动
//
// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //设置启动标志
//
// startActivity(intent); //跳转到意图指定的活动页面
Intent intent = new Intent(); // 创建一个新意图
ComponentName component = new ComponentName(this, ActionUriActivity.class); // 创建包含目标活动在内的组件名称对象
intent.setComponent(component); // 设置意图携带的组件信息
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:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="点击以下按钮将向号码12345发起请求"
android:textColor="@color/black"
android:textSize="17sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_dial"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="跳到拨号页面"
android:textColor="@color/black"
android:textSize="17sp" />
<Button
android:id="@+id/btn_sms"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="跳到短信页面"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
</LinearLayout>

第二个页面代码:
package com.example.myapplication;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class ActionUriActivity extends AppCompatActivity implements View.OnClickListener
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_action_uri);
findViewById(R.id.btn_dial).setOnClickListener(this);
findViewById(R.id.btn_sms).setOnClickListener(this);
}
@Override
public void onClick(View v)
{
String phoneNo = "12345";
if (v.getId() == R.id.btn_dial) // 点击了“跳到拨号页面”按钮
{
Intent intent = new Intent(); // 创建一个新意图
intent.setAction(Intent.ACTION_DIAL); // 设置意图动作为准备拨号
Uri uri = Uri.parse("tel:" + phoneNo); // 声明一个拨号的Uri
intent.setData(uri); // 设置意图前往的路径
startActivity(intent); // 启动意图通往的活动页面
}
else if (v.getId() == R.id.btn_sms) // 点击了“跳到短信页面”按钮
{
Intent intent = new Intent(); // 创建一个新意图
intent.setAction(Intent.ACTION_SENDTO); // 设置意图动作为发短信
Uri uri = Uri.parse("smsto:" + phoneNo); // 声明一个发送短信的Uri
intent.setData(uri); // 设置意图前往的路径
startActivity(intent); // 启动意图通往的活动页面
}
}
}






打开摄像机:
Intent intent = new Intent();
intent.setAction("android.media.action.IMAGE_CAPTURE");
intent.addCategory("android.intent.category.DEFAULT");
startActivity(intent);
发送短信:
Intent intent = new Intent();
intent.setAction("android.intent.action.SENDTO");
intent.addCategory("android.intent.category.DEFAULT");
intent.addCategory("android.intent.category.BROWSABLE");
intent.setData(Uri.parse("smsto:13115925596"));
intent.putExtra("sms_body","Hello World!"); //body是短信数据库短信内容的段名
startActivity(intent);

8600



被折叠的 条评论
为什么被折叠?



