您的位置:首页 > 移动开发 > Android开发

Android中显示意图和隐式意图

2015-10-29 21:59 501 查看


一.什么是意图:
   为减少组件间的耦合,Android提供了Intent(意图),用意图激活其他组件。intent是一种通用的消息系统,允许你的应用程序和其他的应用程序之间传递Intent来执行和产生事件。
   使用Intent可以激活Android应用的三个核心组件:活动,服务广播,和广播接受器。
二.作用是什么?
   1.激活组件
   2.携带数据
   3.意图的匹配(运用到隐式意图)
三.分类:显示意图和隐式意图
   显示意图:

调用Intent setComponent()或者是Intent  setClass()方法明确指定组件名得Intent为显示意图,明确了Intent应该传递给那个组件。

1.创建Intent实例化对象几种方式  

Intent intent = new Intent();  

intent.setClass(Context packageContext, Class<?> cls) ;           //内部调用setComponent(ComponentName)  
intent.setClassName(Context packageContext, String className) ; //内部调用setComponent(ComponentName)  
intent.setClassName(String packageName, String className) ;     //内部调用setComponent(ComponentName),可以激活外部应用  
intent.setComponent(new ComponentName(this, Class<?> cls));  
intent.setComponent(new ComponentName(this, "package Name"));  
隐式意图:

没有明确指定组件名的Intent为隐式意图。系统根据隐式意图中的设置动作(action),类别(category),数据(URI和数据类型)找到合适的组件来处理这个意图。

1).action
The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, 包括Android 系统指定的 和 自定义
[java]  
intent.setAction("com.baidu.action.TEST");  
[html] view plaincopy
<action android:name="com.baidu.action.TEST"/>  
2).data
expressed as a Uri, The data to operate on, such as a person record in the contacts database.
系统自带的Action简单举例
Action Data(Uri) Content
ACTION_VIEW
content://contacts/people/1
Display information about the person whose identifier is "1".
ACTION_VIEW
tel:123
Display the phone dialer with the given number filled in.
ACTION_DIAL
tel:123
Display the phone dialer with the given number filled in.
 自定义data匹配
[java]  
intent.setData(Uri.parse("baidu://www.baidu.com/news"));  
[html]  
<!-- android:path 内容字符串需要以 / 开头 -->  
<data android:scheme="baidu" android:host="www.baidu.com" android:path="/news"/>  
3).category
Gives additional information about the action to execute.
注意:项目清单的xml文件意图过滤器中必须指定 android.intent.category.DEFAULT类别,Activities will very often need to support the CATEGORY_DEFAULT so that they can be found by Context.startActivity,or Context can't the acitivity component
[java]  
intent.addCategory("com.baidu.category.TEST");  
[html] 
<!-- 必须指定CATEGORY_DEFAULT,只有这样startActivity(intent)才能找到 -->  
<category android:name="com.baidu.category.TEST" />  
<category android:name="android.intent.category.DEFAULT" />  
除了以上主要属性外,下面还有其它属性可以额外增强。
4).type
Specifies an explicit type (a MIME type) of the intent data.
[java]  
intent.setType("image/jpeg");  
[html] view plaincopy
<data android:mimeType="image/*" />  
注意:java文件中data Uri 和 type不能同时使用各自的函数进行设定,因为使用type时会把Uri清除掉,可以使用setDataAndType方法设定
[java] 
intent.setDataAndType(Uri.parse("baidu://www.baidu.com/news"), "image/jpeg");  
[html]  
<data android:mimeType="image/*" android:scheme="baidu" android:host="www.baidu.com" android:path="/news"/>  
两者的使用区别:
 显式意图一般在应用的内部使用,因为在应用内部已经知道了组件的名称,直接调用就可以了。
当一个应用要激活另一个应用中的Activity时,只能使用隐式意图,根据Activity配置的意图过滤器建一个意图,让意图中的各项参数的值都跟过滤器匹配,这样就可以激活其他应用中的Activity。所以,隐式意图是在应用与应用之间使用的。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: