还记得我们在文章中多次提及到的页面跳转功能吗,没错,其中java代码中就是通过intent来进行实践的,当时只是简单的提了一下,今天我们来系统的学习一下。
本次的学习内容.
1.intent的相关性质介绍
2.intent的显式应用
3.intent的隐式应用
一、intent的相关性质介绍
Intent是各个组件之间信息沟通的桥梁,既能在Activity之间沟通,又能在Activity和Service之间沟通,也能在Activity与Broadcast之间沟通,总而言之,intent用于Activity各组件之间的通信。
以下是Intent的组成部分:
二、显式Intent的应用
**显式Intent:**显式Intent通过指定具体类名启动一个组件。显式Intent一般用于同一应用程序内,因为可以确定地知道要启动的组件名。另外,Android 5.0以后规定必须显式启动Service。
在Intent的构造函数中指定,示例代码如下:
// 创建一个目标确定的意图
Intent intent = new Intent(this, ActSecondActivity.class);
调用意图对象的setClass方法指定,示例代码如下:
Intent intent = new Intent(); // 创建一个新意图
intent.setClass(this, ActSecondActivity.class); // 设置意图要跳转的目标活动
调用意图对象的setComponent方法指定,示例代码如下:
Intent intent = new Intent(); // 创建一个新意图
// 创建包含目标活动在内的组件名称对象
ComponentName component = new ComponentName(this, ActSecondActivity.class);
intent.setComponent(component); // 设置意图要跳转的目标活动
此外,ComponentName还有一个重载的构造方法,不需要知道class,只需要知道包名packge与类名clss即可跳转,将上述代码中创建组件名称对象改成下面这样即可:
ComponentName component = new ComponentName("包名", "类名");
以下是代码的示例:
xml:
<Button
android:id="@+id/bt5"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginBottom="184dp"
android:onClick="showToast"
android:text="按钮5"
android:textColor="#0968F7"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
java:
Button btn5 = findViewById(R.id.bt5);
btn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
});
MainActivity2的代码任意就好(不报错即可),主要是进行页面的跳转,前面几期文章中也有过多次的实例,也可以去进行参考。这里主要讲一下隐式intent的用法
三、Intent的隐式应用
隐式Intent:隐式Intent无需指定类名,通常用于启动其他应用程序的组件,比如打算启动一个地图定位的Activity。当隐式地启动一个Service或Activity时,Intent会根据其中的内容,匹配其他组件中manifest文件的Intent-filter,启动符合条件的组件,并把Intent中的参数传过去。如果有多个intent-filter满足条件,那么系统会弹出一个对话框,由用户决定启动哪个组件。
比如说,我在游戏的一个页面买了一件装备,我的人物属性页面是如何加上这件装备的属性值的,就是通过intent将装备的属性传给了我的主页面中。
隐式Intent的常用操作:
以下是一些案例:
send.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: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>
send.java:
package com.example.activity;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.example.activity.DateUtil;
public class MainActivity9 extends AppCompatActivity implements View.OnClickListener {
private TextView tv_send; // 声明一个文本视图对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main9);
// 从布局文件中获取名叫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, MainActivity7.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); // 跳转到意图指定的活动页面
}
}
}
receive.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"
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>
receive.java:
package com.example.activity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity7 extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main7);
// 从布局文件中获取名叫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.java:(用来记录实时时间)
package com.example.activity;
import android.annotation.SuppressLint;
import android.text.TextUtils;
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());
}
// 获取当前的日期时间
public static String getNowDateTime(String formatStr) {
String format = formatStr;
if (TextUtils.isEmpty(format)) {
format = "yyyyMMddHHmmss";
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date());
}
}
这样我们就能实时将数据送到另一个activity中,以下是效果图:
从这里就可以看出将一个activity的数据传输到另一个activity中,
双向传输:
这里给个代码就不过多介绍,就是两个页面相互跳转和传输:
respond.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"
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>
repond.java:
package com.example.activity;
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;
import com.example.activity.DateUtil;
public class MainActivity8 extends AppCompatActivity implements View.OnClickListener {
private String mResponse = "我吃过了,还是你来我家吃";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main8);
// 从布局文件中获取名叫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(); // 结束当前的活动页面
}
}
}
send的xml和Java可以试着自己改一下,最后的效果就是一个应答过程:
两个页面都会有实时接受的数据。
尾言
本次的介绍的intent只是其中的冰山一角,正如开头说的,它可以和activity中的多个组件进行配合(比如,权限申请,广播,数据传输,多媒体,定位,网络通信等),未来我会一一和大家介绍,谢谢大家的支持。