Intent.setData vs Intent.putExtra

后端 未结 4 599
长发绾君心
长发绾君心 2020-12-09 15:44

I\'m trying to follow this tutorial:

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

I understand what putExtra does

but I fail to under

相关标签:
4条回答
  • 2020-12-09 16:12

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

    0 讨论(0)
  • 2020-12-09 16:25

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


    putExtra() is mainly used to pass some information to the selected application component,by the Android system.

    0 讨论(0)
  • 2020-12-09 16:27

    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

    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.

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

    0 讨论(0)
  • 2020-12-09 16:30

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

    while .setData is to set the intent's data type.

    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.

    0 讨论(0)
提交回复
热议问题