Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intent.setData vs Intent.putExtra

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.

like image 880
Elad Benda Avatar asked Sep 13 '13 20:09

Elad Benda


People also ask

What is Intent putExtra?

Intents are asynchronous messages which allow Android components to request functionality from other components of the Android system. For example an Activity can send an Intents to the Android system which starts another Activity . putExtra() adds extended data to the intent.

What is setData in Intent in android?

setData() is used for the Android System to find an application component that matches the data attribute in implicit intent.

What is the putExtra () method used with Intent for?

Using putExtra() We can start adding data into the Intent object, we use the method defined in the Intent class putExtra() or putExtras() to store certain data as a key value pair or Bundle data object. These key-value pairs are known as Extras in the sense we are talking about Intents.

What is Intent setType?

setType(String mimeType) input param is represent the MIME type data that u want to get in return from firing intent(here myIntent instance). by using one of following MIME type you can force user to pick option which you desire. Please take a Note here, All MIME types in android are in lowercase.


1 Answers

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

like image 173
Alex.F Avatar answered Sep 19 '22 02:09

Alex.F