我試圖以編程方式在kotlin中發送電子郵件。如何將字符串數組傳遞給android電子郵件意圖
這是我的代碼,我從我在網上找到的一個java例子中翻譯出來。
package com.example.emaildemo
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
class MainActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState:Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val editTextTo:EditText = findViewById(R.id.editText)
val editTextSubject:EditText = findViewById(R.id.editText2)
val editTextMessage:EditText = findViewById(R.id.editText3)
val button1:Button = findViewById(R.id.button)
button1.setOnClickListener(View.OnClickListener{
val to = editTextTo.getText().toString()
val subject = editTextSubject.getText().toString()
val message = editTextMessage.getText().toString()
val intent = Intent(Intent.ACTION_SEND)
val addressees = arrayOf(to)
intent.putExtra(Intent.EXTRA_EMAIL, addressees)
intent.putExtra(Intent.EXTRA_SUBJECT, subject)
intent.putExtra(Intent.EXTRA_TEXT, message)
intent.setType("message/rfc822")
startActivity(Intent.createChooser(intent, "Select Email Sending App :"))
})
}
}
它似乎工作正常,除了「TO」字段沒有複製到用戶的電子郵件應用程序。 Java代碼說:
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{TO});
和Android開發者docs說Intent.EXTRA_EMAIL是「所有的字符串數組‘’收件人的電子郵件地址。」我應該如何定義addressees
變量?
編輯:我發現,如果我硬代碼我的電子郵件地址,
val addressees = arrayOf("[email protected]")
然後應用程序工作正常。當我到達電子郵件應用程序時,我的地址位於「收件人:」字段中,我可以向自己發送郵件。但是,如果我在程序中輸入了相同的地址,它就不會顯示在電子郵件客戶端中。而且,當我在調試器下看它時,意圖從兩種方式顯示完全相同的值。 Curiouser和curiouser。
EDIT2
這是我的XML文件。 (不知怎的,我不能讓前兩行縮進格式正確。)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="To"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="Subject"
android:id="@+id/textView2"
android:layout_below="@+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="Message"
android:id="@+id/textView3"
android:layout_below="@+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="4"
android:id="@+id/editText3"
android:layout_below="@+id/textView3"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Here To Send Email from android application programmatically"
android:id="@+id/button"
android:layout_below="@+id/editText3"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp" />
</RelativeLayout>
你怎麼了? Kotlin'arrayOf(...)'相當於Java'String []'。 –
@ holi-java那麼爲什麼這不工作? – saulspatz
:),我不是一個android開發者,並且你沒有給出任何信息,沒有人知道爲什麼沒有工作。它似乎是你的MIME類型是錯誤的,也許這可以幫助你:https://stackoverflow.com/questions/8701634/send-email-intent –