android - gmail流量限制 - iphone gmail附加檔案
通過Gmail發送電子郵件 (4)
我有一個火災意圖發送電子郵件的代碼
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL,
new String[] { to });
i.putExtra(Intent.EXTRA_SUBJECT, subject);
i.putExtra(Intent.EXTRA_TEXT, msg);
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(Start.this,
"There are no email clients installed.",
Toast.LENGTH_SHORT).show();
}
但是當這個意圖被解僱時,我在列表中看到很多項目,如短信應用程序,gmail應用程序,Facebook應用程序等等。
如何過濾此功能並僅啟用gmail應用程序(或者只是電子郵件應用程序)?
Igor Popov的答案100%正確,但如果你想要一個後備選項,我使用這個方法:
public static Intent createEmailIntent(final String toEmail,
final String subject,
final String message)
{
Intent sendTo = new Intent(Intent.ACTION_SENDTO);
String uriText = "mailto:" + Uri.encode(toEmail) +
"?subject=" + Uri.encode(subject) +
"&body=" + Uri.encode(message);
Uri uri = Uri.parse(uriText);
sendTo.setData(uri);
List<ResolveInfo> resolveInfos =
getPackageManager().queryIntentActivities(sendTo, 0);
// Emulators may not like this check...
if (!resolveInfos.isEmpty())
{
return sendTo;
}
// Nothing resolves send to, so fallback to send...
Intent send = new Intent(Intent.ACTION_SEND);
send.setType("text/plain");
send.putExtra(Intent.EXTRA_EMAIL,
new String[] { toEmail });
send.putExtra(Intent.EXTRA_SUBJECT, subject);
send.putExtra(Intent.EXTRA_TEXT, message);
return Intent.createChooser(send, "Your Title Here");
}
使用android.content.Intent.ACTION_SENDTO
( new Intent(Intent.ACTION_SENDTO);
)只獲取電子郵件客戶端列表,沒有Facebook或其他應用程序。 只是電子郵件客戶端。
我不建議你直接進入電子郵件應用程序。 讓用戶選擇他最喜歡的電子郵件應用。 不要約束他。
如果您使用ACTION_SENDTO,則putExtra無法向主題添加主題和文本。 使用Uri添加主題和正文。
例
Intent send = new Intent(Intent.ACTION_SENDTO);
String uriText = "mailto:" + Uri.encode("[email protected]") +
"?subject=" + Uri.encode("the subject") +
"&body=" + Uri.encode("the body of the message");
Uri uri = Uri.parse(uriText);
send.setData(uri);
startActivity(Intent.createChooser(send, "Send mail..."));
更換
i.setType("text/plain");
同
// need this to prompts email client only
i.setType("message/rfc822");
這是從Android官方文檔中引用的,我在Android 4.4上測試過它,並且運行得很好。 有關更多示例,請訪問https://developer.android.com/guide/components/intents-common.html#Email
public void composeEmail(String[] addresses, String subject) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}