展会信息港展会大全

官方解读Intent之一 android开发教程
来源:互联网   发布日期:2016-01-14 09:44:20   浏览:2528次  

导读:Intent是对即将进行的一项操作的抽象描述。startActivity可以使用它来启动一个Activity,broadcastIntent可以将他发送给任何感兴趣的BroadRecei...

Intent是对即将进行的一项操作的抽象描述。startActivity可以使用它来启动一个Activity,broadcastIntent可以将他发送给任何感兴趣的BroadReceiver组件,并且可以利用startService(Intent)或者bindService(Intent, ServiceConnect, int)和后台的Service进行通信。Intent对于不同的应用之间代码的运行时后绑定提供的便利。它最重要的意义就在于启动activities,这时可以把它理解为多个activities的粘合剂。它最基本的就是对即将进行的操作的抽象描述的一个数据结构。

对于如何创建和解析intents,可以查看Intents and Intent Filters开发指南。

Intent的结构

一个intent中最基本的信息如下:

action:用于描诉此intent进行的动作,例如:ACTION_VIEW,ACTION_EDIT,ACTION_MAIN等等。

data:此intent要操作的数据,例如:用Uri描述的处于联系人数据库中的一条记录。

像这样的action/data对的示例如下:

ACTION_VIEW content://contacts/people/1 -- 显示记录号1的联系人的详细信息。

ACTION_DIAL content://contacts/people/1 -- 显示对记录号位1的联系人的拨号界面。

ACTION_VIEW tel:123 -- 显示对给定的电话号码的拨号界面. VIEW进行的操作是和特定的Uri有关的。

ACTION_DIAL tel:123 -- 显示对给定的电话号码的拨号界面.

ACTION_EDIT content://contacts/people/1 -- 编辑记录号1的联系人的详细信息.

ACTION_VIEW content://contacts/people/ -- 显示一个可浏览的联系人列表. 这个例子是一个联系人应用的典型顶级入口,显示了联系人列表。选中列表中的一条记录,然后利用一个intent{ ACTION_VIEW content://contacts/N }来启动一个activity显示选中记录的详细信息。

除首要属性外,这里有一些第二属性,同样可以包含在一个intent中:

category:关于intent中action要执行的动作的附加描述。例如:DATEGORY——LAUNCHER意味着他必须作为顶级应用的启动器,而GATEGORY_ALTERNATIVE意味着这个intent必须包含再一系列用户可对数据采取的操作的列表之中。

type:指定一个intent的数据的明确的类型(一个MIME类型)。一般情况下,这个类型从数据本身获得。通过设置这个属性,你禁止了这种默认的做法,而是强制制定了一种类型。

component:指定了一个component class的确定的名字,这个类可以被intent使用。通常情况下,这个属性是通过查看intent中的其他信息(action,data/type和categories)并且和一个可以处理它的组件匹配来决定的。如果这个组件被指定了,那么默认的行为就不执行了而是采取被指定的内容。通过指定这个属性,intent的其他属性都变成可选的了。

extras:这个一个包含任何附加信息的Bundle。这个可以用来向组件提供扩展的信息。例如:如果我们有一个发送邮件的动作,我们就可以利用extras来包含邮件的标题和内容等。

这里有一些关于指定Intent附加参数的示例:

ACTION_MAIN with category CATEGORY_HOME -- 启动home screen.

ACTION_GET_CONTENT with MIME type vnd.android.cursor.item/phone -- 显示一个包含电话号码的列表, 允许用户浏览这个列表并且选取其中一条返回到它的parent activity中。

Intent类包含了一系列标准的Intent action和category常量,但是应用程序也可以定义自己的action和category。这些字符串通过使用java style scoping以确保他们是唯一的,例如:标准的ACTION.VIEW就是“android.intent.action.VIEW”

总而言之,ctions, data types, categories, and extra data集合为系统定义了一种语言,允许系统使用像"call john smith's cell"这样的短语。对于加入到系统中的应用程序,他们可以通过添加新的actions,types, and categories来扩展这种语言,或者是可以通过对这些已经存在的短语进行修改来改变这个短语的默认行为。

Intent解析

主要使用的intent有两种形式:

Explicit Intents(命名Intent):这种Intent指定了明确的要运行的组件(通过setComponent(ComponentName)或者是setClass(Context, Class))。通常,这不会包含任何其他的信息,仅仅是应用程序启动内部作为应用程序UI的activities的一种途径。

Implicit Intents(匿名Intent):这种Intent没有指定组件,因此他们本身必须包含足够的信息提供给洗同,让系统去决定哪一个可利用的组件是应该为这个intent启动的。

当使用匿名的intent时,必须提供我们应该对这个intent采取那些动作的信息。这是被Intent resolution(intent 解析)过程来处理的,也就是把这个Intent映射给一个能过处理它的Activity.BroadcastReceiver或者是Service(或者是有的时候给两个或者更多的activities/receivers)。

Intent的解析机制基本上是围绕为一个intent在安装的应用程序包中的所有的<intent-filter>描述寻找相匹配的条目。(另外,在broadcasts这种情况下,通过在那些明确通过registerReceiver(BroadcastReceiver, IntentFilter)注册的任何BroadcastReceiver来匹配,而不是通过<intent-filter>)。

intent中有三个方面的信息是用于Intent解析的:action、type和category。通过这些信息,PackageManager就可以查询到一个可以用来处理这个Intent的组件。合适的组件是通过提供在AndroidManifest.xml文件中的intent信息来决定的,如下所示:

action:如果被给出,必须被作为处理的组件列出来。

type:如果Intent并没有提供这个信息,那么type信息是从Intent的data中获取的。和action一样,如果type再Intent中被包含了(在Intent数据中命令指定或者匿名的),他就必须被作为处理的组件列出来。

如果数据不是一个content:URI并且在intent中没有明确的类型,那么intent中的scheme(例如http:或者mailto:)就会作为替代。还是同action一样,如果我们匹配一个scheme,那么他必须作为可以被处理的组件列出来。

categories:如果被提供了,必须被activity作为它所能处理的categories罗列出来。也就是,如果你包含了一个categories:CATEGORY_LAUNCHER和CATEGORY_ALTERNATIVE,那么你将会仅仅解析包含所有这些categories的intent的组件。Activities将会经常需要提供CATEGORY_DEFAULT以至于他们可以被

Context.startActivity()找到。

举例说明:再NotePad这个示例应用中,它允许用户浏览一个记录notes的列表,也允许用户浏览单个notes的详细信息。以下内容中的斜体字标示你应该用你自己的包名称进行替换。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.android.notepad">

<application android:icon="@drawable/app_notes"

android:label="@string/app_name">

<provider class=".NotePadProvider"

android:authorities="com.google.provider.NotePad" />

<activity class=".NotesList" android:label="@string/title_notes_list">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

<intent-filter>

<action android:name="android.intent.action.VIEW" />

<action android:name="android.intent.action.EDIT" />

<action android:name="android.intent.action.PICK" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />

</intent-filter>

<intent-filter>

<action android:name="android.intent.action.GET_CONTENT" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="vnd.android.cursor.item/vnd.google.note" />

</intent-filter>

</activity>

<activity class=".NoteEditor" android:label="@string/title_note">

<intent-filter android:label="@string/resolve_edit">

<action android:name="android.intent.action.VIEW" />

<action android:name="android.intent.action.EDIT" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="vnd.android.cursor.item/vnd.google.note" />

</intent-filter>

<intent-filter>

<action android:name="android.intent.action.INSERT" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />

</intent-filter>

</activity>

<activity class=".TitleEditor" android:label="@string/title_edit_title"

android:theme="@android:style/Theme.Dialog">

<intent-filter android:label="@string/resolve_title">

<action android:name="com.android.notepad.action.EDIT_TITLE" />

<category android:name="android.intent.category.DEFAULT" />

<category android:name="android.intent.category.ALTERNATIVE" />

<category android:name="android.intent.category.SELECTED_ALTERNATIVE" />

<data android:mimeType="vnd.android.cursor.item/vnd.google.note" />

</intent-filter>

</activity>

</application>

</manifest>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.android.notepad">

<application android:icon="@drawable/app_notes"

android:label="@string/app_name">

<provider class=".NotePadProvider"

android:authorities="com.google.provider.NotePad" />

<activity class=".NotesList" android:label="@string/title_notes_list">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

<intent-filter>

<action android:name="android.intent.action.VIEW" />

<action android:name="android.intent.action.EDIT" />

<action android:name="android.intent.action.PICK" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />

</intent-filter>

<intent-filter>

<action android:name="android.intent.action.GET_CONTENT" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="vnd.android.cursor.item/vnd.google.note" />

</intent-filter>

</activity>

<activity class=".NoteEditor" android:label="@string/title_note">

<intent-filter android:label="@string/resolve_edit">

<action android:name="android.intent.action.VIEW" />

<action android:name="android.intent.action.EDIT" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="vnd.android.cursor.item/vnd.google.note" />

</intent-filter>

<intent-filter>

<action android:name="android.intent.action.INSERT" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />

</intent-filter>

</activity>

<activity class=".TitleEditor" android:label="@string/title_edit_title"

android:theme="@android:style/Theme.Dialog">

<intent-filter android:label="@string/resolve_title">

<action android:name="com.android.notepad.action.EDIT_TITLE" />

<category android:name="android.intent.category.DEFAULT" />

<category android:name="android.intent.category.ALTERNATIVE" />

<category android:name="android.intent.category.SELECTED_ALTERNATIVE" />

<data android:mimeType="vnd.android.cursor.item/vnd.google.note" />

</intent-filter>

</activity>

</application>

</manifest>第一个activity:com.android.notepad.NotesList,作为我们这个应用的主要(首要)入口。它能够做三件事情,正如它的3条intent模板所描述的那样:

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>它为NotePad应用提供了一个顶级入口:标准的MAIN action就是一个主要的入口点(不用查询intent中的其他信息),并且LAUNCHER category表明这个俄入口点需要被列在这个应用程序的启动器中。

<intent-filter>

<action android:name="android.intent.action.VIEW" />

<action android:name="android.intent.action.EDIT" />

<action android:name="android.intent.action.PICK" />

<category android:name="android.intent.category.DEFAULT" />

<data mimeType:name="vnd.android.cursor.dir/vnd.google.note" />

</intent-filter>

<intent-filter> www.2cto.com

<action android:name="android.intent.action.VIEW" />

<action android:name="android.intent.action.EDIT" />

<action android:name="android.intent.action.PICK" />

<category android:name="android.intent.category.DEFAULT" />

<data mimeType:name="vnd.android.cursor.dir/vnd.google.note" />

</intent-filter>以上内容声明了这个activity能够对一个notes目录所进行的操作。能够支持的类型已经再<type>标签中声明了,vnd.android.cursor.dir/vnd.google.note是一个URI,是通过一个可以从保存note pad数据(vnd.google.note)获取0个或多个条目的Cursor构成的。这个activity允许用户查看或者是编辑数据(通过VIEW 和 EDIT cations),或者是获取一个条目,返回到它的调用者那里去(通过PICK action)。z注意,DEFAULT category也在这里提供了:这是被Context.startActivity方法要求的,用于解析你的activity,当activity的名字没有被明确指定的时候。

接下来的内容都是类似的就不进行解释了!

摘自 chenlong12580的专栏

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港