2024年4月11日发(作者:)

intent的用法

Intent的用法

Intent是Android开发中常用的一个类,用于在不同组件之间传

递数据和触发操作。它可以用于在应用内的不同Activity之间进行跳

转,也可以用于调用其他应用的组件。下面将详细介绍Intent的一些

常见用法。

创建Intent对象

使用Intent类创建一个Intent对象,可以通过构造函数传递参

数或使用静态方法创建。

• 通过构造函数传递参数:

Intent intent = new Intent(context, );

其中,context表示当前上下文,TargetActivity表示需要跳转

到的目标Activity。

• 使用静态方法创建:

Intent intent = new Intent();

(context, );

指定Action

Intent可以使用Action来指定要执行的操作。Android提供了一

些内置的Action,也可以自定义Action。以下是一些常见的Action

用法:

• 打开浏览器:

Intent intent = new Intent(_VIEW, ("

startActivity(intent);

• 拨打电话:

Intent intent = new Intent(_DIAL, ("tel:"));

startActivity(intent);

• 发送短信:

Intent intent = new Intent(_SENDTO, ("smsto:"));

("sms_body", "Hello World");

startActivity(intent);

传递数据

Intent可以在不同组件之间传递数据,可以使用putExtra()方法

来传递各种类型的数据。

• 传递基本类型数据:

Intent intent = new Intent(context, );

("key", value);

startActivity(intent);