简体   繁体   中英

How can I send multiple parts of text via Intent?

I have this code snippet:

Intent sharingIntent = new Intent(Intent.ACTION_SENDTO);
sharingIntent.setType("text/plain");
//String  shareBody = (employeeName.getText().toString());
String [] shareBody = {employeeName.getText().toString(), employeeSurname.getText().toString(), absenceType.getText().toString()};
sharingIntent.setData(Uri.parse("mailto:myemail@gmail.com"));
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "ABSENCE CARD");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);

which successfully sends one piece of text accross. How can I include more? This answer didn't provide me with much help. The commented line works for one but the String array doesn't seem to work.

Try serializing and deserializing your string array:

String stringToSent = employeeName.getText().toString()+";"+ employeeSurname.getText().toString()+";"+absenceType.getText().toString();
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, stringToSent);

and deserialize like this:

String[] receivedInfo = new String[2];
receivedInfo = receivedString.split(";");

In your specific case, where you want to send multiple lines in an email, you can also try this: (not tested, may need debugging)

 sharingIntent.setType("text/html"); 
 sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, 
    Html.fromHtml(new StringBuilder()
    .append("<html>")
    .append(employeeName.getText().toString())
    .append("<br/>")
    .append(employeeSurname.getText().toString())
    .append("<br/>")
    .append(absenceType.getText().toString())
    .append("<br/></html>").toString()    
));

Use this code:

ArrayList<String>st= new ArrayList<String>();
st.add("string1"); // Add your text here
st.add("string 2");

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, st);
shareIntent.setType("text/*");
startActivity(Intent.createChooser(shareIntent, "Share text to.."));

Check this link out for more information

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM