Android - Intent

来源:互联网 发布:盐和避难所 mac 中文 编辑:程序博客网 时间:2024/05/17 15:06

Intent is a messaging object that can be used to request an action from another app component.
Three fundamental use-cases:

  1. Start an activity: startActivity(intent) or startActivityForResult(intent, requestCode)
  2. Start a service: startService(intent) or bindService(intent) // bind to service from another component
  3. Deliver a broadcast: A broadcast is a message that any app can receive. You can deliver a broadcast to other apps by passing an Intent to sendBroadcast(), sendOrderedBroadcast(), or sendStickyBroadcast().

1. Intent fields

(a). Component name: we can explicit to give the name of a component that we want to start.

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.


(b). Action: A string that specifies the generic action to perform (such as view or pick).

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_VIEW: 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.
ACTION_SEND: you have some data that the user can share through another app, such as an email app or social sharing app.

Note: 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";

(c). Data: The URI (a Uri object) that references the data to be acted on. 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.

Note: 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.


(d). Category: A string containing additional information about the kind of component that should handle the intent.

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.

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.


(e). 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.

This is often used to transmit data. 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().


(f). Flags: 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).


2. Explicit intent

Explicit intents will specify the component to start by name , when using explicit intent, the system will run this activity or service immedidataly.

For example: (start anotherActivity)

Intent xIntent = new Intent(this, anotherActivity.class);xIntent.setData(Uri.parse(fileUrl));startService(xIntent);

3. Implicit intent

Implicit intent do not name a specific component, but just declare a action that you want to perform, this action can be handled by another component.

When using the implicit intent, system will try to find all appropriate components that can handle this intent by intent filters

If the intent matches an intent filter, the system starts that component and delivers it the Intent object. If multiple intent filters are compatible, the system displays a dialog so the user can pick which app to use.

Implicit Intent mechism

Note: using implicit intent to start a service is not recommended, because you cannot be certain what service will respond to the intent, the service component does not have interface.

For example:

Intent sendIntent = new Intent();sendIntent.setAction(Intent.ACTION_SEND);sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);sendIntent.setType("text/plain");// Verify that the intent will resolve to an activityif (sendIntent.resolveActivity(getPackageManager()) != null) {    startActivity(sendIntent);}

4. Intent filter

Intent filter is used to filter the intents that would like to be received by this component.
If you do not declare any intent filters for an activity, then it can be started only with an explicit intent.

declare one or more intent filters for each of your app components with an “intent-filter” element in your manifest file. Each intent filter specifies the type of intents it accepts based on the intent’s action, data, and category. The system will deliver an implicit intent to your app component only if the intent can pass through one of your intent filters.

Three elements of filter

Note: In order to receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter.

Note: For all activities, you must declare your intent filters in the manifest file. However, filters for broadcast receivers can be registered dynamically by calling registerReceiver(). You can then unregister the receiver with unregisterReceiver(). Doing so allows your app to listen for specific broadcasts during only a specified period of time while your app is running.


(a). Action test:

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

To get through this filter, the action specified in the Intent must match one of the actions listed in the filter. (matching one is enough)

If the filter does not list any actions, there is nothing for an intent to match, so all intents fail the test. However, if an Intent does not specify an action, it will pass the test (as long as the filter contains at least one action).


(b). Category test:

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

For an intent to pass the category test, every category in the Intent must match a category in the filter. (so if all the categories in Intent exist in this filter, that’s fine)

Therefore, an intent with no categories should always pass this test, regardless of what categories are declared in the filter.

Note: Android automatically applies the the CATEGORY_DEFAULT category to all implicit intents passed to startActivity() and startActivityForResult(). So if you want your activity to receive implicit intents, it must include a category for “android.intent.category.DEFAULT” in its intent filters


(c). Data test:

<intent-filter>    <data android:mimeType="video/mpeg" android:scheme="http" ... />    <data android:mimeType="audio/mpeg" android:scheme="http" ... />    ...</intent-filter>

URI structure has separate attributes-
scheme: //host:port/path

For example: http: // yiqi.example.project:8080/dir/tex/etc
scheme: http
host: yiqi.example.project
port: 8080
path: dir/tex/etc

Each of these attributes is optional in a data element, but there are linear dependencies:
• If a scheme is not specified, the host is ignored.
• If a host is not specified, the port is ignored.
• If both the scheme and host are not specified, the path is ignored.

When the URI in an intent is compared to a URI specification in a filter, it’s compared only to the parts of the URI included in the filter. For example:
• If a filter specifies only a scheme, all URIs with that scheme match the filter.
• If a filter specifies a scheme and an authority but no path, all URIs with the same scheme and authority pass the filter, regardless of their paths.
• If a filter specifies a scheme, an authority, and a path, only URIs with the same scheme, authority, and path pass the filter.

Note: A path specification can contain a wildcard asterisk (*) to require only a partial match of the path name.

0 0
原创粉丝点击