答雨落秋垣
通过 Intent 调用 Activity 的完整指南
在 Android 开发中,Intent 是组件间通信的核心机制。以下是使用 Intent 调用 Activity 的详细方法:
一、基础调用方式
1. 显式 Intent(明确指定目标 Activity)
// 在当前Activity中启动另一个Activity
Intent intent = new Intent(this, TargetActivity.class);
startActivity(intent);
2. 隐式 Intent(通过动作和数据类型调用)
// 调用系统浏览器打开网页
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(" https://www.example.com "));
startActivity(intent);
// 调用拨号界面
Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:123456789"));
startActivity(dialIntent);
二、传递数据
1. 传递简单数据
Intent intent = new Intent(this, TargetActivity.class);
// 添加附加数据
intent.putExtra("key_string", "Hello World");
intent.putExtra("key_int", 123);
intent.putExtra("key_boolean", true);
startActivity(intent);
2. 在目标Activity中接收数据
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String stringData = intent.getStringExtra("key_string");
int intData = intent.getIntExtra("key_int", 0); // 0是默认值
boolean boolData = intent.getBooleanExtra("key_boolean", false);
}
三、高级用法
1. 传递复杂对象(需实现 Parcelable 或 Serializable)
实现 Parcelable 的示例:
public class User implements Parcelable {
private String name;
private int age;
// 实现Parcelable接口的方法
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
}
public static final Creator<User> CREATOR = new Creator<User>() {
@Override
public User createFromParcel(Parcel in) {
return new User(in);
}
@Override
public User[] newArray(int size) {
return new User[size];
}
};
protected User(Parcel in) {
name = in.readString();
age = in.readInt();
}
}
传递和接收:
// 发送方
User user = new User("Alice", 25);
Intent intent = new Intent(this, TargetActivity.class);
intent.putExtra("user", user);
startActivity(intent);
// 接收方
User receivedUser = getIntent().getParcelableExtra("user");
2. 启动Activity并期待返回结果
启动Activity:
Intent intent = new Intent(this, TargetActivity.class);
startActivityForResult(intent, REQUEST_CODE); // REQUEST_CODE是自定义的请求码
在目标Activity中设置返回结果:
Intent resultIntent = new Intent();
resultIntent.putExtra("result_key", "返回的数据");
setResult(RESULT_OK, resultIntent); // RESULT_OK是结果码
finish();
在原Activity中接收结果:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
String result = data.getStringExtra("result_key");
// 处理返回结果
}
}
四、特殊场景调用
1. 调用其他应用的Activity
// 调用地图应用显示位置
Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
// 验证是否有应用能处理此Intent
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mapIntent);
}
2. 使用Intent过滤器
在AndroidManifest.xml中为目标Activity声明:
<activity android:name=".MyActivity">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
然后可以通过以下方式调用:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "这是要分享的文本");
sendIntent.setType("text/plain");
startActivity(sendIntent);
五、最佳实践
总是检查Intent是否能被处理:
if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } else { // 处理没有应用能响应Intent的情况 }
使用Intent常量:
// 使用系统定义的常量而非硬编码字符串 intent.setAction(Intent.ACTION_VIEW);
考虑安全性:
对接收的Intent数据进行验证
使用适当的权限保护你的Activity
处理多窗口模式:
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT); // 或 intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
使用Intent的Flags:
// 清除任务栈 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); // 禁止返回 intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
通过掌握这些Intent的使用方法,你可以灵活地在Android应用中启动Activity并实现组件间的通信。