2012-01-24 128 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