Intent的概念

说明:翻译自google android参考手册,有错误的地方请指正。

 

 

一个intent是对将要执行的一个操作的抽象描述,也就是说,这个intent对象里面包含的数据告诉系统你要做什么,系统根据你所设置的数据去执行动作。startActivity启动一个Activity实例的时候可以使用一个intent作为参数;broadcastIntent能够发送一个intent给关注这个intent的任何BroadcastReceiver组件;startService(Intent)或者bindService(Intent,ServiceConnection,int)则跟一个后台Service通信。

它最重要的作用就是用于启动一个activity,可以把intent看做不同的activity的粘合剂,也就是说,它作为一个activity启动一个新的activity的媒介。一个intent的信息的主要部分有:

  • action--将要被执行的动作,例如,ACTION_VIEW,ACTION_EDIT,ACTION_MAIN等;
  • data--这个动作所要操作的数据,例如一个联系人数据库中的一个个人记录,表现形式为一个Uri。

下面是一些action/data对的示例:

  • ACTION_VIEW content://contacts/people/1 -- 显示标识符为“1”的person记录的信息;

  • ACTION_DIAL content://contacts/people/1 -- 显示标识符为“1”的person的电话号码的phone dialer;

  • ACTION_VIEW tel:123 -- Display the phone dialer with the given number filled in. Note how the VIEW action does what what is considered the most reasonable thing for a particular URI.

  • ACTION_DIAL tel:123 -- Display the phone dialer with the given number filled in.

  • ACTION_EDIT content://contacts/people/1 -- 编辑标识符为“1”的person记录的信息;

  • ACTION_VIEW content://contacts/people/ -- 显示一个person记录列表。这个例子在contacts程序中是很常用的,用户选择列表中某一个人之后将导致一个新的intent{ACTION_VIEW content://contacts/N}被执行,从而启动一个新的显示这个人信息的activity。

 除了上述这些重要的特性之外,一个intent中也可以包含相对来说次要一点的属性:

 

  1. category -- 给出了所要执行的action的额外信息。例如,CATEGORY_LAUNCHER表示这个intent必须在Launcher中作为一个top-level应用程序出现,而CATEGORY_ALTERNATIVE表示这个intent作为用户对某一块数据的一个可替代操作列表中。
  2. type -- 指定intent data的一个显式类型(MIME类型)。通常data的类型是由它自己指定的。如果你给出了这个属性的值,那么将会强制使用一个显示的类型,系统不会自己推断。
  3. component -- 指定一个component类的一个显式的名字。Normally this is determined by looking at the other information in the intent (the action, data/type, and categories) and matching that with a component that can handle it 。如果这个属性被设置,那么系统就不会自己去推断,并且这个组件将会被显式使用。使用了这个属性,其他的intent属性将变得可选。
  4. extras -- 这是任何额外信息的一个Bundle。这个属性可以为组件提供扩展信息。例如,如果我们有一个动作是发送e-mail,我们可以在这儿提供这封e-mail的subject或者body等信息。

这儿是使用了次要属性的一些例子:

  1. ACTION_MAIN with category CATEGORY_HOME -- Launch the home screen.

  2. ACTION_GET_CONTENT with MIME type vnd.android.cursor.item/phone -- Display the list of people's phone numbers, allowing the user to browse through them and pick one and return it to the parent activity.

  3. ACTION_GET_CONTENT with MIME type */* and category CATEGORY_OPENABLE -- Display all pickers for data that can be opened with ContentResolver.openInputStream(), allowing the user to pick one of them and then some data inside of it and returning the resulting URI to the caller. This can be used, for example, in an e-mail application to allow the user to pick some data to include as an attachment.

Intent类中提供了很多标准的Intent action和category常量,但我们可以定义我们自己的。这些字符串使用的是java style scoping,从而确保它们是唯一的——例如,ACTION_VIEW被称为“android.intent.action.VIEW”。

 

 

Intent Resolution

我们所要使用的intent主要有两种类型:

 

  • Explicit Intents 通过setComponent(ComponentName) 或者 setClass(Context, Class)指定确切的类来运行一个组件。通常,这些不包含其他的信息,是一个应用程序在用户与其交互的时候运行所拥有的多个内部activities的一个简单的方法。

  • Implicit Intents 不明确指定一个组件的类; 而是提供足够的信息供系统自己推断运行哪一个可用组件。

 

使用implicit intents的时候,我们需要知道提供哪些信息,这些信息是被Intent resolution的process管理的。Intent resolution的process能够映射一个Intent到一个Activity,BroadcastReceiver或者Service,有时是二或者多个activities/receivers。

 

The intent resolution mechanism basically revolves around matching an Intent against all of the <intent-filter> descriptions in the installed application packages. (Plus, in the case of broadcasts, any BroadcastReceiver objects explicitly registered with registerReceiver(BroadcastReceiver, IntentFilter).) More details on this can be found in the documentation on the IntentFilter class.

 

一个Intent中有三方面的信息被用于Intent Resolution:action,type和category。使用这些信息,就能在PackageManager中查找出处理这个intent的组件。系统根据AndroidManifest.xml文件中提供的intent信息来确定合适的组件。AndroidManifest.xml中提供的信息如下:

 

  • The action, if given, must be listed by the component as one it handles.一个组件如果能处理这个Action,那么它必须列出。

  • The type。如果intent中没有提供type,那么必须根据数据来检索。如果intent(either explicitly or implicitly in its data)包含了一个type,那么一个能处理这种type数据的组件必须列出。

  • For data that is not a content: URI and where no explicit type is included in the Intent, instead the scheme of the intent data (such as http: or mailto:) is considered. Again like the action, if we are matching a scheme it must be listed by the component as one it can handle.
  • The categories, if supplied, must all be listed by the activity as categories it handles. That is, if you include the categories CATEGORY_LAUNCHER and CATEGORY_ALTERNATIVE, then you will only resolve to components with an intent that lists both of those categories. Activities will very often need to support the CATEGORY_DEFAULT so that they can be found by Context.startActivity().

 

例如,一个记事本程序,它允许用户浏览notes data的一个列表,并且能够查看独立项的详情。下面这些东西准备待进一步了解之后再翻译。

 

 

 

The first activity, com.android.notepad.NotesList, serves as our main entry into the app. It can do three things as described by its three intent templates:

 

1.

This provides a top-level entry into the NotePad application: the standard MAIN action is a main entry point (not requiring any other information in the Intent), and the LAUNCHER category says that this entry point should be listed in the application launcher.

 

2.

This declares the things that the activity can do on a directory of notes. The type being supported is given with the <type> tag, where vnd.android.cursor.dir/vnd.google.note is a URI from which a Cursor of zero or more items (vnd.android.cursor.dir) can be retrieved which holds our note pad data (vnd.google.note). The activity allows the user to view or edit the directory of data (via the VIEW and EDIT actions), or to pick a particular note and return it to the caller (via the PICK action). Note also the DEFAULT category supplied here: this is required for the Context.startActivity method to resolve your activity when its component name is not explicitly specified.

3.

  1. This filter describes the ability return to the caller a note selected by the user without needing to know where it came from. The data type vnd.android.cursor.item/vnd.google.note is a URI from which a Cursor of exactly one (vnd.android.cursor.item) item can be retrieved which contains our note pad data (vnd.google.note). The GET_CONTENT action is similar to the PICK action, where the activity will return to its caller a piece of data selected by the user. Here, however, the caller specifies the type of data they desire instead of the type of data the user will be picking from.

Given these capabilities, the following intents will resolve to the NotesList activity:

  • { action=android.app.action.MAIN } matches all of the activities that can be used as top-level entry points into an application.

  • { action=android.app.action.MAIN, category=android.app.category.LAUNCHER } is the actual intent used by the Launcher to populate its top-level list.

  • { action=android.intent.action.VIEW data=content://com.google.provider.NotePad/notes } displays a list of all the notes under "content://com.google.provider.NotePad/notes", which the user can browse through and see the details on.

  • { action=android.app.action.PICK data=content://com.google.provider.NotePad/notes } provides a list of the notes under "content://com.google.provider.NotePad/notes", from which the user can pick a note whose data URL is returned back to the caller.

  • { action=android.app.action.GET_CONTENT type=vnd.android.cursor.item/vnd.google.note } is similar to the pick action, but allows the caller to specify the kind of data they want back so that the system can find the appropriate activity to pick something of that data type.

The second activity, com.android.notepad.NoteEditor, shows the user a single note entry and allows them to edit it. It can do two things as described by its two intent templates:

1.

The first, primary, purpose of this activity is to let the user interact with a single note, as decribed by the MIME type vnd.android.cursor.item/vnd.google.note. The activity can either VIEW a note or allow the user to EDIT it. Again we support the DEFAULT category to allow the activity to be launched without explicitly specifying its component.

2.

  1. The secondary use of this activity is to insert a new note entry into an existing directory of notes. This is used when the user creates a new note: the INSERT action is executed on the directory of notes, causing this activity to run and have the user create the new note data which it then adds to the content provider.

Given these capabilities, the following intents will resolve to the NoteEditor activity:

  • { action=android.intent.action.VIEW data=content://com.google.provider.NotePad/notes/{ID} } shows the user the content of note {ID}.

  • { action=android.app.action.EDIT data=content://com.google.provider.NotePad/notes/{ID} } allows the user to edit the content of note {ID}.

  • { action=android.app.action.INSERT data=content://com.google.provider.NotePad/notes } creates a new, empty note in the notes list at "content://com.google.provider.NotePad/notes" and allows the user to edit it. If they keep their changes, the URI of the newly created note is returned to the caller.

The last activity, com.android.notepad.TitleEditor, allows the user to edit the title of a note. This could be implemented as a class that the application directly invokes (by explicitly setting its component in the Intent), but here we show a way you can publish alternative operations on existing data:

In the single intent template here, we have created our own private action called com.android.notepad.action.EDIT_TITLE which means to edit the title of a note. It must be invoked on a specific note (data type vnd.android.cursor.item/vnd.google.note) like the previous view and edit actions, but here displays and edits the title contained in the note data.

In addition to supporting the default category as usual, our title editor also supports two other standard categories: ALTERNATIVE and SELECTED_ALTERNATIVE. Implementing these categories allows others to find the special action it provides without directly knowing about it, through the queryIntentActivityOptions(ComponentName, Intent[], Intent, int) method, or more often to build dynamic menu items with addIntentOptions(int, int, int, ComponentName, Intent[], Intent, int, MenuItem[]). Note that in the intent template here was also supply an explicit name for the template (via android:label="@string/resolve_title") to better control what the user sees when presented with this activity as an alternative action to the data they are viewing.

Given these capabilities, the following intent will resolve to the TitleEditor activity:

  • { action=com.android.notepad.action.EDIT_TITLE data=content://com.google.provider.NotePad/notes/{ID} } displays and allows the user to edit the title associated with note {ID}.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值