Intent用于处理Android各组件之间的通信,地位自然相当重要。我们在开发中也会时常用到,但是很多人对他的了解确并不透彻。今天我大概总结一些和Intent相关的知识点。
1.Intent工作主要有3部分
- Intent需标明本次通信请求从哪里来、到哪里去、要怎么走。
- 发起方携带本次通信需要的数据内容,接收方对收到的Intent数据进行解包。
- 如果发起方要求判断接收方的处理结果,Intent就要负责让接收方传回应答的数据内容。
2.Intent相关参数
元素名称 | 设置方法 | 说明与用途 |
Component | setComponent | 组件,用于指定Intent的来源与目的 |
Action | setAction | 动作,用于指定Intent的操作行为 |
Data | setData | 即Uri,用于指定动作要操纵的数据路径 |
Category | addCategory | 类别,用于指定Intent的操作类别 |
Type | setType | 数据类型,用于指定消息的数据类型 |
Extras | putExtra | 扩展信息,用于指定装载的参数信息 |
Flags | setFlags | 标志位,用于指定Intent的运行模式(启动标志) |
3.显式Intent的3种调用方式
(1) 在构造函数中指定,示例代码如下:
Intent intent = new Intent(this,ActResponseActivity.class);//创建一个目标确定的意图
(2)调用setClass方法指定,示例代码如下:
Intent intent = new Intent();//创建一个新意图
intent.setClass(this,ActResponseActivity.class);//设置意图要跳转的活动类
(3)调用setComponent方法指定,示例代码如下:
Intent intent = new Intent();//创建一个意图
ComponentName component = new ComponentName(this,ActResponseActivity.class);
intent.setComponent(component);//设置意图携带的组件信息
4.隐式Intent的系统动作
Intent类的系统动作常量名 | 系统动作的常量值 | 说明 |
ACTION_MAIN | android.intent.action.MAIN | App启动时的入口 |
ACTION_VIEW | android.intent.action.VIEW | 显示数据给用户 |
ACTION_EDIT | android.intent.action.EDIT | 显示可编辑的数据 |
ACTION_SEND | android.intent.action.SEND | 分享内容 |
ACTION_CALL | android.intent.action.CALL | 直接拨号 |
ACTION_DIAL | android.intent.action.DIAL | 准备拨号 |
ACTION_SENDTO | android.intent.action.SENDTO | 发送短信 |
ACTION_ANSWER | android.intent.action.ANSWER | 接听电话 |
ACTION_SEARCH | android.intent.action.SEARCH | 导航栏上SearchView的搜索动作 |
5.拨打电话举例(直接拨通)
Intent intent = new Intent();//创建一个新意图
intent.setAction(Intent.ACTION_CALL);//设置意图动作为直接拨号
Uri uri = Uri.parse("tel:"+phone);//申明一个拨号的Uri
intent.setData(uri);//设置意图前往的路径
startActivity(intent);//启动意图通往的活动页面
6.入口动作配置
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<actegory android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
7.隐式Intent发送短信
Intent intent = new Intent();
intent.setAction("android.intent.action.SENDTO");
intent.setData(Uri.parse("smsto:"+"15536935920"));
intent.putExtra("sms_body","小熊熊写代码发送的短信");
startActivity(intent);
8.拨打电话举例(待拨通)
Intent intent = new Intent();//创建一个新意图
intent.setAction(Intent.ACTION_DIAL);//设置意图动作为直接拨号
Uri uri = Uri.parse("tel:"+"15835133570");//申明一个拨号的Uri
intent.setData(uri);//设置意图前往的路径
startActivity(intent);//启动意图通往的活动页面
9.传递参数到下一个页面
Intent intent = new Intent(MainActivity.this,FirstActivity.class);//创建一个目标确定的意图
Bundle bundle = new Bundle();//创建一个新包裹
bundle.putString("name","张三");//往包裹存入一个字符串
bundle.putInt("age",30);//往包裹里存入一个整型数
bundle.putDouble("height",170.0f);//往包裹存入一个双精度数
intent.putExtras(bundle);//把快递包裹塞给意图
startActivity(intent);//启动意图所向往的活动页面
10.接收上一个页面传递的参数
Intent intent = getIntent();//获取前一个页面传来的意图
Bundle bundle = intent.getExtras();//卸下意图里的快递包裹
String name = bundle.getString("name","");//从包裹里取出字符串
int age = bundle.getInt("age",0);//从包裹里取出整数
double height = bundle.getDouble("height",0.0f);//从包裹里取出双精度数
11.向上一个页面返回参数
Intent intent = new Intent();//创建一个新意图
Bundle bundle = new Bundle();//创建一个新包裹
bundle.putString("job","码农");//往包裹存入一个字符串
intent.putExtras(bundle);//把快递包裹塞给意图
setResult(Activity.RESULT_OK,intent);//携带意图返回前一个页面
finish();//关闭当前页面
12.上一个页面接收参数
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bundle resp = data.getExtras();
String job = resp.getString("job");
}