当涉及到 Android 开发中的 Intent(意图)时,它是一个非常重要的概念,用于在组件之间传递数据和启动组件(如 Activity、Service、BroadcastReceiver)。以下是 Intent 的详细总结,包括示例代码和详细注释,以帮助你更好地理解这个概念。
1. 创建 Intent
你可以使用 Intent 类来创建一个意图对象,然后配置它以执行特定操作。Intent 可以是显式的(指定目标组件)或隐式的(指定操作和数据,让系统选择匹配的组件)。
显式 Intent 示例:
// 创建一个显式 Intent,指定目标 Activity
Intent explicitIntent = new Intent(this, 目标活动.class);
// 换种写法二
//创建一个 Intent 对象。
Intent intent = new Intent();
//使用 setClass() 方法将要启动的活动的类名设置为 Intent 对象的 class 属性。
intent.setClass(this, 目标活动.class);
//使用 startActivity() 方法启动活动。
startActivity(intent);
//换种写法三
// 创建一个 Intent 并设置目标组件的类名
Intent intent = new Intent();
// 设置组件的包名和类名(TargetActivity)
ComponentName componentName = new ComponentName("com.example.包名","com.example.myapp.类名");
// 使用 setComponent() 方法设置目标组件
intent.setComponent(componentName);
// 启动目标 Activity
startActivity(intent);
隐式 Intent 示例:
// 创建一个隐式 Intent,指定操作和数据
Intent implicitIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.example.com"));
常量名 | 常量值 | 说明 |
---|---|---|
ACTION_VIEW | "android.intent.action.VIEW" | 查看操作,用于打开指定的数据,如网址、文件等。 |
ACTION_EDIT | "android.intent.action.EDIT" | 编辑操作,用于编辑指定的数据,如联系人信息、文本等。 |
ACTION_SEND | "android.intent.action.SEND" | 发送操作,用于发送数据,如文本、图像、文件等。 |
ACTION_DIAL | "android.intent.action.DIAL" | 拨号操作,用于打开拨号器并填充指定的号码。 |
ACTION_CALL | "android.intent.action.CALL" | 拨打电话操作,直接拨打指定的电话号码。 |
ACTION_PICK | "android.intent.action.PICK" | 选择操作,用于选择一个特定类型的数据,如联系人或图片。 |
ACTION_GET_CONTENT | "android.intent.action.GET_CONTENT" | 获取内容操作,类似于 PICK,用于获取特定类型的内容。 |
ACTION_MAIN | "android.intent.action.MAIN" | 主操作,用于指定一个应用程序的主入口点。 |
ACTION_SENDTO | "android.intent.action.SENDTO" | 发送到操作,用于发送数据到指定的地址,如电子邮件地址、短信。 |
ACTION_WEB_SEARCH | "android.intent.action.WEB_SEARCH" | Web 搜索操作,用于触发设备的 Web 搜索功能。 |
ACTION_POWER_CONNECTED | "android.intent.action.ACTION_POWER_CONNECTED" | 插入电源时触发的广播。 |
ACTION_POWER_DISCONNECTED | "android.intent.action.ACTION_POWER_DISCONNECTED" | 断开电源时触发的广播。 |
ACTION_BATTERY_LOW | "android.intent.action.BATTERY_LOW" | 低电量警告广播。 |
ACTION_BATTERY_OKAY | "android.intent.action.BATTERY_OKAY" | 电量恢复正常广播。 |
ACTION_AIRPLANE_MODE_CHANGED | "android.intent.action.AIRPLANE_MODE_CHANGED" | 飞行模式状态改变广播。 |
ACTION_SCREEN_ON | "android.intent.action.SCREEN_ON" | 屏幕开启广播。 |
ACTION_SCREEN_OFF | "android.intent.action.SCREEN_OFF" | 屏幕关闭广播。 |
ACTION_TIME_TICK | "android.intent.action.TIME_TICK" | 每分钟发送一次的广播。 |
2. 添加额外数据
你可以使用 Intent 来传递额外的数据,这些数据以键值对的形式存储。
Intent intent = new Intent(this, 目标活动.class);
intent.putExtra("key", "value");
3. 启动组件
你可以使用 Intent 启动其他组件,如 Activity、Service 或 BroadcastReceiver。
startActivity(intent); // 启动一个 Activity
startService(intent); // 启动一个 Service
sendBroadcast(intent); // 发送广播
4. 获取传递的数据
在目标组件中,你可以使用 Intent 获取传递的数据。
// 在目标 Activity 中获取传递的数据
Intent intent = getIntent();
String value = intent.getStringExtra("key");
5. 过滤器和匹配
隐式 Intent 使用过滤器(Filter)来确定哪个组件会响应它。过滤器包含操作、数据和类别等信息。
xmlCopy code<!-- 在 AndroidManifest.xml 中定义 Activity 的过滤器 -->
<activity android:name=".目标活动">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="https" android:host="www.example.com" />
</intent-filter>
</activity>
6. Intent 的种类
- 显式 Intent:明确指定了目标组件的类名,用于启动特定的组件。
- 隐式 Intent:指定了操作和数据,系统会根据这些信息选择合适的组件。
- 带有返回数据的 Intent:启动目标组件后,可以通过
startActivityForResult()
启动,目标组件可以设置结果并返回给调用者。
7. Intent 的使用场景
组成部分 | 使用方法 | 说明和用途 |
---|---|---|
Action(操作) | setAction(String action) | 设置 Intent 的操作,描述要执行的任务或动作。常用于指定要执行的功能,如查看、编辑、发送、拨号等。 |
Data(数据) | setData(Uri uri) | 设置 Intent 的数据,通常是一个 Uri 对象,表示操作要处理的数据,如网址、文件路径等。 |
Type(类型) | setType(String type) | 设置 Intent 的数据类型,用于描述数据的 MIME 类型,指示数据的内容类型,如文本、图像、音频等。 |
Component(组件) | setComponent(ComponentName component) setClass(Context packageContext, Class<?> cls) | 设置目标组件的信息,包括包名和类名,用于显式启动特定的 Activity、Service 或 BroadcastReceiver。 |
Category(类别) | addCategory(String category) | 添加 Intent 的类别,用于进一步描述操作的类型,如默认操作、选择操作等。 |
Flags(标志) | addFlags(int flags) setFlags(int flags) | 添加或设置 Intent 的标志,自定义其行为,如指定启动模式、设置额外标志等。 |
Extras(额外数据) | putExtra(String name, Type value) getExtra(String name) | 通过 putExtra() 方法添加额外数据,通过 getExtra() 方法在目标组件中获取传递的额外数据,用于传递参数和信息。 |
- 启动新的 Activity 或打开链接。
- 调用系统服务(如相机、联系人、地图等)。
- 发送广播以与其他应用程序通信。
- 启动后台服务执行长时间运行的任务。
8. 示例代码
以下是一个简单的示例,演示了如何创建、配置和使用 Intent。假设有两个 Activity:MainActivity 和 TargetActivity。
MainActivity.java:
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void open目标活动(View view) {
// 创建一个显式 Intent,启动 目标活动,并传递额外数据
Intent intent = new Intent(this, 目标活动.class);
intent.putExtra("message", "你好,我来自 MainActivity!");
startActivity(intent);
}
}
TargetActivity.java:
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class TargetActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_target);
// 获取传递的数据
Intent intent = getIntent();
String message = intent.getStringExtra("message");
// 显示传递的数据
TextView textView = findViewById(R.id.textView);
textView.setText(message);
}
}
在这个示例中,MainActivity 创建了一个显式 Intent,并传递了一个字符串消息给 目标活动。目标活动 获取并显示了传递的消息。
希望这个详细的解释和示例有助于你理解 Android 中的 Intent 概念。 Intent 是 Android 应用程序中实现组件之间通信的重要工具。