眾所周知,在Android中調用其他程序進行相關處理,都是使用的Intent。當然,Email也不例外。
在Android中,調用Email有三種類型的Intent:
Intent.ACTION_SENDTO 無附件的發送
Intent.ACTION_SEND 帶附件的發送
Intent.ACTION_SEND_MULTIPLE 帶有多附件的發送
當然,所謂的調用Email,隻是說Email可以接收Intent並做這些事情,可能也有其他的應用程序實現瞭相關功能,所以在執行的時候,會出現選擇框進行選擇。
1.使用SENTTO發送
[java]
Intent data=new Intent(Intent.ACTION_SENDTO);
data.setData(Uri.parse("mailto:455245521@qq.com"));
data.putExtra(Intent.EXTRA_SUBJECT, "這是標題");
data.putExtra(Intent.EXTRA_TEXT, "這是內容");
startActivity(data);
通過向Intent中putExtra來設定郵件的相關參數。
2.使用SEND發送
[java]
Intent intent = new Intent(Intent.ACTION_SEND);
String[] tos = { "fdafdafa@gmail.com" };
String[] ccs = { "gegeff@gmail.com" };
String[] bccs = {"fdafda@gmail.com"};
intent.putExtra(Intent.EXTRA_EMAIL, tos);
intent.putExtra(Intent.EXTRA_CC, ccs);
intent.putExtra(Intent.EXTRA_BCC, bccs);
intent.putExtra(Intent.EXTRA_TEXT, "body");
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/Chrysanthemum.jpg"));
intent.setType("image/*");
intent.setType("message/rfc882");
Intent.createChooser(intent, "Choose Email Client");
startActivity(intent);
很簡單,發送郵件中,有收件者,抄送者,密送者。 也就是分別通過
Intent.EXTRA_EMAIL,
Intent.EXTRA_CC,
Intent.EXTRA_BCC
來進行putExtra來設定的。
而單個附件的發送,則使用Intent.EXTRA_STREAM來設置附件的地址Uri。
3.使用SEND_MULTIPLE來進行多附件的發送
[java]
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
String[] tos = { "wingfourever@gmail.com" };
String[] ccs = { "tongyue@gmail.com" };
intent.putExtra(Intent.EXTRA_EMAIL, tos);
intent.putExtra(Intent.EXTRA_CC, ccs);
intent.putExtra(Intent.EXTRA_TEXT, "body");
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
ArrayList<Uri> imageUris = new ArrayList<Uri>();
imageUris.add(Uri.parse("file:///sdcard/Chrysanthemum.jpg"));
imageUris.add(Uri.parse("file:///sdcard/Desert.jpg"));
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
intent.setType("image/*");
intent.setType("message/rfc882");
Intent.createChooser(intent, "Choose Email Client");
startActivity(intent);
發送多個附件,最主要的時候,通過putParcelableArrayListExtra將多個附件的Uri地址List設置進去就OK瞭。其實還是很簡單的。
如下是在三星galaxy tab 2 10.1上面的運行效果:
對於使用郵件發送,在很多的Android應用中都會使用到,跟微博分享一樣的常見。大傢也隻需要稍微瞭解下就可以瞭,畢竟還是很容易的。
轉載請註明出處:http://blog.csdn.net/ml3947/