Android putExtra()和setData()之间的差异
putExtra()和setData()之间有什么区别?我读过安卓文档,但没有多大帮助。 还有一个先前的问题,但仍然不清楚。Android putExtra()和setData()之间的差异,android,android-intent,Android,Android Intent,putExtra()和setData()之间有什么区别?我读过安卓文档,但没有多大帮助。 还有一个先前的问题,但仍然不清楚。 提前感谢。putExtra允许您添加基本(或可打包)键值对setData仅限于传递Uri设置数据通常用于从其他来源请求数据的情况,例如在startActivityForResult中。setData() 设置此意图操作的数据。此方法自动清除以前由setType(字符串)或setTypeAndNormalize(字符串)设置的任何类型 注意:Android框架中的方案匹配区
提前感谢。putExtra允许您添加基本(或可打包)键值对
setData
仅限于传递Uri
<代码>设置数据通常用于从其他来源请求数据的情况,例如在startActivityForResult中。setData()
设置此意图操作的数据。此方法自动清除以前由setType(字符串)或setTypeAndNormalize(字符串)设置的任何类型
注意:Android框架中的方案匹配区分大小写,与正式的RFC不同。因此,您应该始终使用小写方案编写Uri,或者使用normalizeScheme()或setDataAndNormalize(Uri)确保方案转换为小写
参数
数据:此意图现在针对的数据的Uri
意图用于向Android系统发出特定事件已经发生的信号。意图通常描述应执行的操作,并提供应执行此类操作的数据。例如,您的应用程序可以通过特定URL的浏览器组件启动。下面的示例演示了这一点
String url = "http://www.google.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
但是安卓系统如何识别能够对特定意图做出反应的组件呢
为此,使用了意图过滤器的概念。意图过滤器指定活动、服务或广播接收器可以响应的意图类型。因此,它声明了组件的功能
Android组件在AndroidManifest.xml中静态注册意向过滤器,或者在广播接收器的情况下也通过代码动态注册意向过滤器。意图过滤器由其类别、操作和数据过滤器定义。它还可以包含其他元数据
如果意向被发送到Android系统,Android平台将使用包含在意向对象中的数据运行接收器确定。在这种情况下,它确定为意图数据注册的组件。如果多个组件已为同一意向过滤器注册,则用户可以决定应启动哪个组件
putExtra()
将扩展数据添加到意图中
参数:
name:额外数据的名称
value:字符串数组数据值
返回相同的Intent对象,用于将多个调用链接到单个语句中。查看源代码:
/**
* 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;
}
/**
*设置此意图操作的数据。这种方法是自动的
*清除以前由{@link#setType}或
*{@link#setTypeAndNormalize}。
*
*注意:Android框架中的方案匹配是
*区分大小写,与正式RFC不同。因此,
*您应该始终使用小写方案编写Uri,
*或者使用{@linkuri#normalizeScheme}或者
*{@link#setDataAndNormalize}
*确保方案转换为小写。
*
*@param data此意图现在针对的数据的Uri。
*
*@return返回相同的Intent对象,用于链接多个调用
*变成一句话。
*
*@see#getData
*@see#setDataAndNormalize
*@see android.net.Uri#normalizeScheme()
*/
公共意图集数据(Uri数据){
mData=data;//私有Uri mData
mType=null;//私有字符串mType;
归还这个;
}
/**
*将扩展数据添加到意图中。名称必须包含一个包
*前缀,例如app com.android.contacts将使用名称
*比如“com.android.contacts.ShowAll”。
*
*@param name额外数据的名称,带有包前缀。
*@param value字符串数据值。
*
*@return返回相同的Intent对象,用于链接多个调用
*变成一句话。
*
*@see#putExtras
*@see#removeExtra
*@see#getStringExtra(字符串)
*/
public Intent putExtra(字符串名称、字符串值){
if(mExtras==null){
mExtras=新的Bundle();
}
putString(名称、值);
归还这个;
}
setData()
用于传递数据,必须对其采取操作;而putExtra()
将发送有关操作的额外信息
例如,如果一个人正在启动一个活动来执行
动作\u调用
,那么他必须设置调用setData()
的号码。如果他想传递任何其他额外信息,那么他应该使用putExtra()
检查文档和这个,android.os.Bundle),但是uri也可以通过putExtra发送。是的,但是get/setData不需要“key”-put/getExtra-do。set/getData实际上只是一个简写,表示不必拿出每个应用程序都知道的密钥。请您详细解释一下“设置此意图所使用的数据”的含义??谢谢。。这在某种程度上澄清了问题。。但是假设我想要启动一个明确的意图,那么在setData中设置什么呢??在这种情况下,我可以使用putextra提供uri吗?直接从Skillgun复制,但简短且非常精确: