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。所以,隐式意图是在应用与应用之间使用的。
相关文章推荐
- AndroidTest
- Android Touch事件的分发响应机制
- Android Studio中Button ,Textview中字体自动变成大写的问题
- AndroidStudio实用插件收集
- 解决Android抽屉被击穿问题
- 日报2015/10/29(极客学院安卓视频学习)
- Android 配置文件 manifest 子节点属性说明
- Android中的“再按一次返回键退出程序”实现
- Android实现推送方式解决方案
- 【Android】实现打开关闭效果
- Android抽屉(SlidingDrawer --类似android通知栏下拉效果)
- 用户注册--Android
- 【Android成长之路】全局大喇叭——广播机制的浅谈(发送自定义广播)
- Android开发手记(18) 数据存储三 SQLite存储数据
- Android JS相互调用详解
- Android基础入门教程——10.6 PowerManager(电源服务)
- 使用Android Studio进行NDK开发
- android studio使用jcenter上的开源库
- android 计算器,纯手打,功能实现了,bug可能还有点
- Android Studio Tips -- 提取方法