Java Intent.setData 与 Intent.putExtra

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18794504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:08:03  来源:igfitidea点击:

Intent.setData vs Intent.putExtra

javaandroidapiandroid-intent

提问by Elad Benda

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 Docs,没有太大帮助:

Set the data this intent is operating on.

Set the data this intent is operating on.

what does this mean for the constant

这对常数意味着什么

intent.setData(CalendarContract.Events.CONTENT_URI);?

intent.setData(CalendarContract.Events.CONTENT_URI);?

There doesn't seem to be any affect when I comment out this line.

当我注释掉这一行时,似乎没有任何影响。

采纳答案by Alex.F

setData()is used to point to the locationof 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 Intentsand the setData(), setType()and setDataAndType()

有关更多信息,我建议阅读一些有关IntentsetData(),setType()setDataAndType()

回答by Ahmed Ekri

I think that .putExtrais to transfer a string or something. like Aramex :P

我认为.putExtra是传递字符串什么的。像 Aramex :P

while .setDatais to set the intent's data type.

while.setData是设置意图的数据类型。

see in the intent it's 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.

看到它的意图Intent.ACTION_INSERT。所以它正在等待插入的东西。这就是你设置数据的原因。.setData(CalendarContract.Events.CONTENT_URI);您插入了日历事件。

回答by Henok Tesfaye

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系统向选中的应用组件传递一些信息。

回答by Daniel

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. - When that information is a data location that can be represented by a URI.

使用意图数据字段 (Intent.setData): - 当您只有一条信息需要发送到已启动的活动时。- 当该信息是可以由 URI 表示的数据位置时。

Use the intent extras (Intent.putExtra): - If you want to pass more than one piece of information to the started activity. - If any of the information you want to pass is not expressible by a URI.

使用 Intent extras (Intent.putExtra): - 如果您想将多条信息传递给已启动的活动。- 如果您要传递的任何信息不能通过 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.

意图数据和附加内容不是排他性的;您可以将 data 用于 URI,并将 extras 用于启动活动处理该 URI 中的数据所需的任何附加信息。