Intent.setData 与 Intent.putExtra [英] Intent.setData vs Intent.putExtra

查看:30
本文介绍了Intent.setData 与 Intent.putExtra的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照本教程进行操作:

http://www.vogella.com/articles/AndroidCalendar/article.html>

我了解 putExtra 的作用

但我不明白 setData() 是什么意思?

Android Docs,没有太大帮助:

设置这个意图正在操作的数据.

这对常数意味着什么

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

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

解决方案

setData() 用于指向数据对象的位置(例如用于例如),而 putExtra() 添加简单数据类型(例如 SMS 文本字符串).

这里有两个例子来澄清:

setData() 此处用于设置要共享的文件的位置.

File fileToShare = new File("/sdcard/somefile.dat");Intent i = new Intent();i.setAction(Intent.ACTION_SEND);i.setData(Uri.fromFile(fileToShare));开始活动(i);

putExtra() 此处用于设置要分享的文本内容.

Intent i = new Intent();i.setAction(Intent.ACTION_SEND);String textBodyString = "一些文字";i.putExtra(Intent.EXTRA_TEXT, textBodyString);i.setType(HTTP.PLAIN_TEXT_TYPE);

有关更多信息,我建议阅读有关意图setData()setType()setDataAndType()

I'm trying to follow this tutorial:

http://www.vogella.com/articles/AndroidCalendar/article.html

I understand what putExtra does

but I fail to understand what setData() means?

Android Docs, wasn't much helpful:

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

Here are two examples to clarify:

setData() used here to set the location of a file that you want to share.

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.

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

这篇关于Intent.setData 与 Intent.putExtra的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆