Intent官方教程(3)各属性介绍

Building an Intent

  An Intent object carries information that the Android system uses to determine which component to start (such as the exact component name or component category that should receive the intent), plus information that the recipient component uses in order to properly perform the action (such as the action to take and the data to act upon).
  The primary information contained in an Intent is the following:

  • Component name 目标组件名

  The name of the component to start. 
  This is optional, but it's the critical piece of information that makes an intent explicit, meaning that the intent should be delivered only to the app component defined by the component name. Without a component name, the intent is implicit and the system decides which component should receive the intent based on the other intent information (such as the action, data, and category—described below). So if you need to start a specific component in your app, you should specify the component name.

  可选项。目标组件全包名字,一但指定后就只能启动目标组件。启动服务时最好用组件名启动。

  Note: When starting a Service, you should always specify the component name. Otherwise, you cannot be certain what service will respond to the intent, and the user cannot see which service starts.
  This field of the Intent is a ComponentName object, which you can specify using a fully qualified class name of the target component, including the package name of the app. For example, com.example.ExampleActivity. You can set the component name with setComponent(), setClass(), setClassName(), or with the Intent constructor.

  • Action 动作

  A string that specifies the generic action to perform (such as view or pick). 
  In the case of a broadcast intent, this is the action that took place and is being reported. The action largely determines how the rest of the intent is structured—particularly what is contained in the data and extras. 
  You can specify your own actions for use by intents within your app (or for use by other apps to invoke components in your app), but you should usually use action constants defined by the Intent class or other framework classes. Here are some common actions for starting an activity:

  可以自定义一个Action,下面是两个常见的启动Activity的行为。

  ACTION_VIEW
    Use this action in an intent with startActivity() when you have some information that an activity can show to the user, such as a photo to view in a gallery app, or an address to view in a map app.

  可显示view

  ACTION_SEND
    Also known as the "share" intent, you should use this in an intent with startActivity() when you have some data that the user can share through another app, such as an email app or social sharing app.

  发送到:蓝牙,邮箱,短信等。更多动作见:Intent class reference for more constants

  See the Intent class reference for more constants that define generic actions. Other actions are defined elsewhere in the Android framework, such as in Settings for actions that open specific screens in the system's Settings app.
  You can specify the action for an intent with setAction() or with an Intent constructor.
  If you define your own actions, be sure to include your app's package name as a prefix. For example:

  自定义动作举例:
 static final String ACTION_TIMETRAVEL = "com.example.action.TIMETRAVEL";
  • Data 数据类型

  The URI (a Uri object) that references the data to be acted on and/or the MIME type of that data. The type of data supplied is generally dictated by the intent's action. For example, if the action is ACTION_EDIT, the data should contain the URI of the document to edit. 

  Data属性有两部分构成: 数据URI 和 数据MIME type 

  When creating an intent, it's often important to specify the type of data (its MIME type) in addition to its URI. For example, an activity that's able to display images probably won't be able to play an audio file, even though the URI formats could be similar. So specifying the MIME type of your data helps the Android system find the best component to receive your intent. However, the MIME type can sometimes be inferred from the URI—particularly when the data is a content: URI, which indicates the data is located on the device and controlled by a ContentProvider, which makes the data MIME type visible to the system.
  To set only the data URI, call setData(). To set only the MIME type, call setType(). If necessary, you can set both explicitly with setDataAndType().
  Caution: If you want to set both the URI and MIME type, do not call setData() and setType() because they each nullify the value of the other. Always use setDataAndType() to set both URI and MIME type.

  • Category 类别

  A string containing additional information about the kind of component that should handle the intent. Any number of category descriptions can be placed in an intent, but most intents do not require a category. Here are some common categories: 分类举例如下:

  CATEGORY_BROWSABLE
    The target activity allows itself to be started by a web browser to display data referenced by a link—such as an image or an e-mail message.
  CATEGORY_LAUNCHER
    The activity is the initial activity of a task and is listed in the system's application launcher.

  See the Intent class description for the full list of categories.
  You can specify a category with addCategory().
  These properties listed above (component name, action, data, and category) represent the defining characteristics of an intent. By reading these properties, the Android system is able to resolve which app component it should start.

  However, an intent can carry additional information that does not affect how it is resolved to an app component. An intent can also supply:

  • Extras 附加数据

  Key-value pairs that carry additional information required to accomplish the requested action. Just as some actions use particular kinds of data URIs, some actions also use particular extras. 
  You can add extra data with various putExtra() methods, each accepting two parameters: the key name and the value. You can also create a Bundle object with all the extra data, then insert the Bundle in the Intent with putExtras().
  For example, when creating an intent to send an email with ACTION_SEND, you can specify the "to" recipient with the EXTRA_EMAIL key, and specify the "subject" with the EXTRA_SUBJECT key.

  The Intent class specifies many EXTRA_* constants for standardized data types. If you need to declare your own extra keys (for intents that your app receives), be sure to include your app's package name as a prefix. For example:

 static final String EXTRA_GIGAWATTS = "com.example.EXTRA_GIGAWATTS";
  • Flags 标记

  Flags defined in the Intent class that function as metadata for the intent. The flags may instruct the Android system how to launch an activity (for example, which task the activity should belong to) and how to treat it after it's launched (for example, whether it belongs in the list of recent activities). 

  For more information, see the setFlags() method.

 

posted @ 2016-09-24 22:45  f9q  阅读(282)  评论(0编辑  收藏  举报