所有属性
Component name
用来设置组件名称,例如要跳转到的Activity
可以使用setComponent进行设置
Intent intent = new Intent();
ComponentName componentName = new ComponentName("com.example.study2", "com.example.study2.MainActivity2");
intent.setComponent(componentName);
startActivity(intent);
Action和Data
动作和数据一般绑定在一起使用
Action常量
Action一般是用一些常量指定他的行为。可以从官方文档看到
Data
对于不同的Action就有不同的Data。如下ACTION_VIEW,表示浏览网页,那么Data就是一个网址,而对于ACTION_CALL,表示是拨打电话,那么Data就是电话号码,而ACTION_EDIT就是编辑,那么Data就是编辑的内容
实例
1、需求:使用Action实现打电话和发短信
2、activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打电话"
></Button>
<Button
android:id="@+id/sms"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发短信"
></Button>
</LinearLayout>
3、MainActivity.java
package com.example.action_phone_mail_page;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button phone = findViewById(R.id.phone);
Button sms = findViewById(R.id.sms);
phone.setOnClickListener(l);
sms.setOnClickListener(l);
}
View.OnClickListener l = new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
switch (view.getId()){
case R.id.phone:
intent.setAction(Intent.ACTION_DIAL);
// 配置电话
intent.setData(Uri.parse("tel:15977745684"));
startActivity(intent);
break;
case R.id.sms:{
intent.setAction(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:15977745683"));
// 配置发送信息
intent.putExtra("sms_body", "hello world");
startActivity(intent);
break;
}
}
}
};
}
4、在AndroidMainifest.xml中添加拨打电话和发送信息权限
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
Action和Category
可以启用系统的Activity和启动其他应用的组件
Categrory
执行的动作的类别,他也是通过一些常量进行配置。可以从官方文档看到
如下是CATEGORY_LANUNCH表示配置程序的入口,这里也可以从AndroidManifest.xml文件可以看到
除此以外还有CATEGORY_HOME这个常量,表示动作为返回桌面
实例
1、需求:模拟关闭应用返回桌面
2、activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button
android:id="@+id/close"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="关闭"
></Button>
</LinearLayout>
3、MainActivity.java
package com.example.action_phone_mail_page;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button close = findViewById(R.id.close);
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(intent.CATEGORY_HOME);
startActivity(intent);
}
});
}
}
通过官方文档可以看到ACTION_MAIN和CATEGORY_HOME可以返回到系统桌面
Extras属性
用于在Intent中添加附加信息,通常用于Activity之间进行交换数据,详细可以看这里Activity详解
Flags属性
用来指示如何启用另一个Activity,指示程序启动后如何处理Activity。他是通过一些常量进行指定的,可以在官方文档中找到这些常量,是以FLAG开头
例如指示是否在近期的Activity列表中,一般而言,一个页面内跳转到另一个Activity,如果我们进行点击手机的HOME键返回原程序,这个时候会显示的是跳转到的Activity。但是可以通过Flags的FLAG_ACTIVITY_NO_HISTORY常量进行取消这种行为
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
intent.setFlags(intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);