这里有新鲜出炉的精品教程,程序狗速度看过来!
Android 是一种基于 Linux 的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由 Google 公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用 "安卓" 或 "安致"。
这篇文章主要介绍了 Android 隐式 Intent 的实例详解的相关资料, 隐式意图就是在不明确设置激活对象的前提下寻找最匹配的组件,需要的朋友可以参考下
Android 隐式 Intent 的实例详解
前言:
顾名思义,隐式意图就是在不明确设置激活对象的前提下寻找最匹配的组件,举个例子,比如有 5 个人:
(1)A:170cm
(2)B:160cm
(3)C:180cm
(4)D:190cm
(5)E:200cm
如果是显示意图的话,如果我们要指明选择 A 的话会说:"我选择 A.",但是如果是隐式意图,则会说:"我要选择 170cm 的人",虽然没有指明要选 A,但会寻找条件最匹配的人。
在 intent 过滤器中类似于上面例子中的 "身高" 条件的匹配条件有:
(1)action
(2)category
(3)data:scheme、host、path、type
当在程序中设置了这些激活组件的条件,程序就会去寻找最匹配的组件, 但是注意:只要有一点不匹配,则就是不匹配;
比如:
- Intent intent = new Intent();
- intent.setAction("a");//此句只是指定了Action
- startActivity(intent);//寻找最匹配的组件激活,内部会调用intent.addCategory("Android.intent.category.DEFAULT");
隐式 Intent 的核心代码
首先是在 AndroidManifest.xml 中为某个 Activity 设置意图过滤器:
- <activity>
- <intent-filter>
- <action android:name="...."/>
- <category android:name="...."/>
- <category android:name="android.intent.category.DEFAULT"/> <!--此句一般都要加 -->
- <data android:scheme="..." android:host="..." android:path="/..." android:type="..."/>
- </intent-filter>
- </activity>
以上设置是设置 Activity 本身的属性,接下来在程序中要设置的是我们要寻找时匹配的条件:
(1)Intent intent = new Intent();
(2)intent.setAction("....");
(3)intent.addCategory("....");
(4)intent.setData(Uri.parse("...."));// 设置 data 的 scheme、host、path 条件
(5)intent.setDataAndType(Uri.parse(""),String type);// 同时设置 data 的 scheme、host、path、type 条件
(6)startActivity(intent);// 调用 intent.addCategory("android.intent.category.DEFAULT");
以上就是 Android 隐式 Intent 的详解,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮到大家,谢谢大家对本站的支持!
来源: http://www.phperz.com/article/17/0818/344205.html