[英]Intent.setData vs Intent.putExtra
I'm trying to follow this tutorial: 我正在尝试按照本教程:
http://www.vogella.com/articles/AndroidCalendar/article.html http://www.vogella.com/articles/AndroidCalendar/article.html
I understand what putExtra does 我理解putExtra的作用
but I fail to understand what setData() means? 但我不明白setData()的含义是什么?
Android Docs, wasn't much helpful: Android文档,没什么用处:
Set the data this intent is operating on.
what does this mean for the constant 这对常数意味着什么
intent.setData(CalendarContract.Events.CONTENT_URI);
? ?
There doesn't seem to be any affect when I comment out this line. 当我注释掉这一行时似乎没有任何影响。
setData()
is used to point to the location of a data object (like a file for example), while putExtra()
adds simple data types (such as an SMS text string for example). setData()
用于指向数据对象的位置 (例如文件),而putExtra()
添加简单数据类型 (例如SMS文本字符串)。
Here are two examples to clarify: 以下是两个澄清的例子:
setData()
used here to set the location of a file that you want to share. setData()
用于设置要共享的文件的位置。
File fileToShare = new File("/sdcard/somefile.dat");
Intent i = new Intent();
i.setAction(Intent.ACTION_SEND);
i.setData(Uri.fromFile(fileToShare));
startActivity(i);
putExtra()
is used here to set the text content that you want to share. putExtra()
用于设置要共享的文本内容。
Intent i = new Intent();
i.setAction(Intent.ACTION_SEND);
String textBodyString = "some text";
i.putExtra(Intent.EXTRA_TEXT, textBodyString);
i.setType(HTTP.PLAIN_TEXT_TYPE);
For more information I suggest some readings about Intents and the setData()
, setType()
and setDataAndType()
有关更多信息,我建议阅读有关Intents和
setData()
, setType()
和setDataAndType()
setData() is used for the Android System to find an application component that matches the data attribute in implicit intent. setData()用于Android系统,以查找与隐式intent中的data属性匹配的应用程序组件。
putExtra() is mainly used to pass some information to the selected application component,by the Android system. putExtra()主要用于通过Android系统将一些信息传递给选定的应用程序组件。
I think that .putExtra
is to transfer a string or something. 我认为
.putExtra
是传输字符串或其他东西。 like Aramex :P 像Aramex:P
while .setData
is to set the intent's data type. 而
.setData
用于设置intent的数据类型。
see in the intent it's Intent.ACTION_INSERT
. 在意图中看到它是
Intent.ACTION_INSERT
。 So it's waiting for something to be inserted. 所以它正在等待插入的东西。 That's why you set the data.
这就是你设置数据的原因。
.setData(CalendarContract.Events.CONTENT_URI);
You inserted the calendar events. 您插入了日历事件。
I've found a good answer here: https://google-developer-training.gitbooks.io/android-developer-fundamentals-course-concepts/content/en/Unit%201/21_c_understanding_activities_and_intents.html 我在这里找到了一个很好的答案: https : //google-developer-training.gitbooks.io/android-developer-fundamentals-course-concepts/content/en/Unit%201/21_c_understanding_activities_and_intents.html
Use the intent data field (Intent.setData): - When you only have one piece of information you need to send to the started activity. 使用意图数据字段(Intent.setData): - 当您只有一条信息需要发送到已启动的活动时。 - When that information is a data location that can be represented by a URI.
- 当该信息是可以由URI表示的数据位置时。
Use the intent extras (Intent.putExtra): - If you want to pass more than one piece of information to the started activity. 使用intent extras(Intent.putExtra): - 如果要将多条信息传递给已启动的活动。 - If any of the information you want to pass is not expressible by a URI.
- 如果您要传递的任何信息不能通过URI表达。
Intent data and extras are not exclusive; 意图数据和附加内容不是唯一的; you can use data for a URI and extras for any additional information the started activity needs to process the data in that URI.
您可以将URI的数据和附加内容用于已启动的活动处理该URI中的数据所需的任何其他信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.