2012-12-19 90 views
0

EnquiryAndroid:如何將額外字符串傳遞給電子郵件?

這裏用戶必須輸入他/她的姓名,年齡和消息。它必須打開Gmail應用程序,顯示輸入的名稱/年齡/消息。它只顯示MESSAGE。剩餘的細節我沒有得到它。

String s1= name.getText().toString(); 
    String s2= age.getText().toString(); 
    String s3= msg.getText().toString(); 
    Intent email = new Intent(Intent.ACTION_SEND); 
    email.putExtra(Intent.EXTRA_EMAIL, 
    new String[] { "[email protected]" }); 
    email.putExtra(Intent.EXTRA_SUBJECT,"hiiiii"); 
    email.putExtra("Name", s1); 
    email.putExtra("Age", s2); 
    email.putExtra(Intent.EXTRA_TEXT, s3); 
    email.setType("message/rfc822"); 
    startActivity(Intent.createChooser(email,"Choose an Email client :")); 

回答

0

那是因爲類型EMAIL沒有名稱或年齡的概念。電子郵件僅包含收件人,發件人,郵件和主題。您必須連結您的姓名和年齡數據,並將其放入主題或消息中。像這樣的東西可以工作:

String s1= name.getText().toString(); 
String s2= age.getText().toString(); 
String s3= msg.getText().toString(); 
Intent email = new Intent(Intent.ACTION_SEND); 
email.putExtra(Intent.EXTRA_EMAIL, 
new String[] { "[email protected]" }); 
email.putExtra(Intent.EXTRA_SUBJECT,"hiiiii"); 
email.putExtra("Name", s1); 
email.putExtra("Age", s2); 
nameAgeStr = "Name: " + s1 + " Age: " + s2 + ".\n"; 
email.putExtra(Intent.EXTRA_TEXT,nameAgeStr + s3); 
email.setType("message/rfc822"); 
startActivity(Intent.createChooser(email,"Choose an Email client :")); 

這將把名稱和年齡放在他們自己的行上郵件的正文(消息)。

1

Gmail不包含名稱和年齡字段。

如果您想將它們附加到您的消息正文,您可以將它們附加到s4msg字符串,並將它們全部作爲消息一起傳遞。

s4msg = s1name + "\n" + s2age + "\n" + s4msg 
email.putExtra(Intent.EXTRA_TEXT, s4msg); 
1

在打開郵件應用程序之前,您需要格式化郵件正文。例如:

String messageTemplate = "Hi %1$s\nThis is your message: %2$s"; 
String mailBody = String.format(messageTemplate, name.getText().toString(), msg.getText().toString()); 

Intent email = new Intent(Intent.ACTION_SEND); 
email.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected]" }); 
email.putExtra(Intent.EXTRA_SUBJECT,"hiiiii"); 
email.putExtra(Intent.EXTRA_TEXT, mailBody); 
email.setType("message/rfc822"); 
startActivity(Intent.createChooser(email,"Choose an Email client :")); 
相關問題