-->

Difference between putExtra() and setData()

2020-08-17 09:48发布

问题:

What is the difference between putExtra() and setData()? I have read the android docs but it is of not much help. Also there is a previous question Intent.setData vs Intent.putExtra but it is still not clear. Thanks in advance.

回答1:

setData()

Set the data this intent is operating on. This method automatically clears any type that was previously set by setType(String) or setTypeAndNormalize(String).

Note: scheme matching in the Android framework is case-sensitive, unlike the formal RFC. As a result, you should always write your Uri with a lower case scheme, or use normalizeScheme() or setDataAndNormalize(Uri) to ensure that the scheme is converted to lower case.

Parameters

data: The Uri of the data this intent is now targeting.

Intents are used to signal to the Android system that a certain event has occurred. Intents often describes the action which should be performed and provide data on which such an action should be done. For example your application can start via an intent a browser component for a certain URL. This is demonstrated by the following example.

    String url = "http://www.google.com";
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i); 

But how does the Android system identify the components which can react to a certain intent?

For this the concept of an intent filter is used. An intent filter specifies the types of intents to which that an activity , service, or broadcast receiver can respond to. It declares therefore the capabilities of a component.

Android components register intent filters either statically in the AndroidManifest.xml or in case of a broadcast receiver also dynamically via code. An intent filter is defined by its category, action and data filters. It can also contain additional metadata.

If an intent is send to the Android system, the Android platform runs, using the data included in the Intent object, an receiver determination. In this it determines the components which are registered for the data of the intent. If several components have registered for the same intent filter the user can decide which component should be started.

putExtra()

Add extended data to the intent.

Parameters:

name: The name of the extra data.

value: The String array data value.

Returns the same Intent object, for chaining multiple calls into a single statement.



回答2:

putExtra allows you to add primitive (or parcelable) key-value pairs. setData is limited to passing an Uri. setData is conventionally used for the case of requesting data from another source, such as in startActivityForResult.



回答3:

take a look at the source:

/**
 * Set the data this intent is operating on.  This method automatically
 * clears any type that was previously set by {@link #setType} or
 * {@link #setTypeAndNormalize}.
 *
 * <p><em>Note: scheme matching in the Android framework is
 * case-sensitive, unlike the formal RFC. As a result,
 * you should always write your Uri with a lower case scheme,
 * or use {@link Uri#normalizeScheme} or
 * {@link #setDataAndNormalize}
 * to ensure that the scheme is converted to lower case.</em>
 *
 * @param data The Uri of the data this intent is now targeting.
 *
 * @return Returns the same Intent object, for chaining multiple calls
 * into a single statement.
 *
 * @see #getData
 * @see #setDataAndNormalize
 * @see android.net.Uri#normalizeScheme()
 */
public Intent setData(Uri data) {
    mData = data;  // private Uri mData
    mType = null;  // private String mType;
    return this;
}

/**
 * Add extended data to the intent.  The name must include a package
 * prefix, for example the app com.android.contacts would use names
 * like "com.android.contacts.ShowAll".
 *
 * @param name The name of the extra data, with package prefix.
 * @param value The String data value.
 *
 * @return Returns the same Intent object, for chaining multiple calls
 * into a single statement.
 *
 * @see #putExtras
 * @see #removeExtra
 * @see #getStringExtra(String)
 */
public Intent putExtra(String name, String value) {
    if (mExtras == null) {
        mExtras = new Bundle();
    }
    mExtras.putString(name, value);
    return this;
}


回答4:

setData() is to pass data on which, action has to be taken; while putExtra() is to send extra information about the action.

For example, if one is starting an activity to perform ACTION_CALL, then he has to set the number to call in setData(). And if he wants to pass any other extra information, then he should use putExtra().