2012-01-24 45 views
2

我一直在使用Java和Xml几个月,现在已经学会了很多,谢谢大家对StackOverflow的帮助。Android按钮onclick提交至电子邮件

我的问题是关于与编程提交按钮的android的java编程。

目前,我试图找出如何将一个值提交到电子邮件地址(幕后)

假设我们有一个文本字段和按钮;我想在文本字段中输入值,并将其提交到onclick的电子邮件地址。

我在网上找不到任何东西,告诉我如何做到这一点。

非常感谢您阅读我的文章,我期待您的建议。

+0

; http://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-android-ap – aacanakin

回答

8

这是如何使用Intents可以非常方便的一个很好的例子! Android有一大堆预定义的intents在系统中执行某些事情;您可能在之前点击过一张图片,弹出一个对话框,询问您是希望在图库还是第三方应用程序(如Astro)中查看它。观看图像有其自己的预定意图。

发送电子邮件也有其自己的预定意图:android.content.Intent.ACTION_SEND。您需要使用该属性创建一个意图,然后附加额外的信息(即要发送到的地址,主题/邮件正文等)。

示例代码:

// Data members 
private Intent emailIntent; 
private String feedback; 
private EditText feedbackBox; 

// Create the Intent, and give it the pre-defined value 
// that the Android machine automatically associates with 
// sending an email. 
emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
emailIntent.setType("plain/text"); 

// Put extra information into the Intent, including the email address 
// that you wish to send to, and any subject (optional, of course). 
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"[email protected]"}); 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Insert subject here"); 

// Acquire feedback from an EditText and save it to a String. 
feedback = feedbackBox.getText().toString(); 

// Put the message into the Intent as more extra information,     
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, feedback); 

// Start the Intent, which will launch the user's email 
// app (make sure you save any necessary information in YOUR app 
// in your onPause() method, as launching the email Intent will 
// pause your app). This will create what I discussed above - a 
// popup box that the user can use to determine which app they would like 
// to use in order to send the email. 
startActivity(Intent.createChooser(emailIntent, "Insert title for dialog box.")); 

我希望这有助于!

你可能想看看一些来源:
http://developer.android.com/guide/topics/intents/intents-filters.html
http://developer.android.com/reference/android/content/Intent.html#ACTION_SEND

+0

感谢您的帮助,我将于明天上午尝试此操作,并将结果返回。 – MADPADGE

+1

是的,这确实有帮助!感谢你的榜样! – MADPADGE

1
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, someEditText.getText()); 
startActivity(Intent.createChooser(emailIntent, "Send someone an email...")); 
+0

谢谢你的帮助,我会试试这个AM。我会回复结果。 – MADPADGE

0

试试这个

Intent intent = new Intent(Intent.ACTION_SENDTO); 
    intent.setData(Uri.parse("mailto:Type email address here")); 
    if (intent.resolveActivity(getPackageManager()) != null) { 
     startActivity(intent); 
    } 
如果你想使用Gmail,看看这个帖子
+0

请编辑更多信息。仅限代码和“尝试这个”的答案是不鼓励的,因为它们不包含可搜索的内容,也不解释为什么有人应该“尝试这个”。 – abarisone