如何使用Intent在Android中發送電子郵件含代碼

如何使用Intent在Android中發送電子郵件

我們可以通過Intent在Android中輕松發送電子郵件。愛掏網 - it200.com您只需按照以下給出的幾行代碼即可。愛掏網 - it200.com

Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.putExtra(Intent.EXTRA_TEXT, message);

//need this to prompts email client only
email.setType("message/rfc822");

startActivity(Intent.createChooser(email, "Choose an Email client :"));

Android中的電話呼叫示例

activity_main.xml

從工具欄中拖動2個EditTexts,1個多行EditText,3個TextViews和1個Button,現在activity_main.xml文件將如下所示:

<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="22dp"
        android:layout_marginTop="16dp"
        android:ems="10" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="18dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText2"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="28dp"
        android:ems="10"
        android:inputType="textMultiLine" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText1"
        android:layout_alignBottom="@+id/editText1"
        android:layout_alignParentLeft="true"
        android:text="To:" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText2"
        android:layout_alignBottom="@+id/editText2"
        android:layout_alignParentLeft="true"
        android:text="Subject:" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText3"
        android:layout_alignBottom="@+id/editText3"
        android:layout_alignParentLeft="true"
        android:text="Message:" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText3"
        android:layout_below="@+id/editText3"
        android:layout_marginLeft="76dp"
        android:layout_marginTop="20dp"
        android:text="Send" />

</RelativeLayout>

Activity類

讓我們編寫代碼通過Intent發送電子郵件。愛掏網 - it200.com

package com.example.sendemail;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
    EditText editTextTo,editTextSubject,editTextMessage;
    Button send;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editTextTo=(EditText)findViewById(R.id.editText1);
        editTextSubject=(EditText)findViewById(R.id.editText2);
        editTextMessage=(EditText)findViewById(R.id.editText3);

        send=(Button)findViewById(R.id.button1);

        send.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                 String to=editTextTo.getText().toString();
                 String subject=editTextSubject.getText().toString();
                 String message=editTextMessage.getText().toString();


                 Intent email = new Intent(Intent.ACTION_SEND);
                  email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
                  email.putExtra(Intent.EXTRA_SUBJECT, subject);
                  email.putExtra(Intent.EXTRA_TEXT, message);

                  //need this to prompts email client only
                  email.setType("message/rfc822");

                  startActivity(Intent.createChooser(email, "Choose an Email client :"));

            }

        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
發表評論
更多 網友評論0 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 日本www高清视频| 精品乱码久久久久久久| 无码福利一区二区三区| 可以免费看污视频的网站| www激情com| 欧美精品videossex欧美性| 国产精品成人va在线观看| 九九久久99综合一区二区| 色噜噜狠狠狠狠色综合久不| 少妇丰满爆乳被呻吟进入| 亚洲毛片在线免费观看| 日本高清在线免费| 无码丰满熟妇一区二区| 你是我的女人中文字幕高清| 9277手机在线视频观看免费| 最近高清日本免费| 国产18禁黄网站免费观看| caoporn进入| 欧美两性人xxxx高清免费| 国产亚洲欧美另类一区二区三区| 一女被两男吃奶玩乳尖| 欧美日韩一区二区综合 | 亚洲人配人种jizz| 日本一区二区三区久久| 最近中文字幕网2019| 国产人妖一区二区| www.11yinyuan.com| 欧美丰满大乳大屁股流白浆| 国产一区二区精品人妖系列| 99精品偷自拍| 日韩国产成人精品视频人| 免费黄色a级片| 中文乱码字幕午夜无线观看| 成品煮伊在2021一二三久| 亚洲熟妇色自偷自拍另类| 非洲人zoxxxx另类| 大屁股熟女一区二区三区| 久久综合九色综合97免费下载| 精品国偷自产在线| 国产福利1000| 一区二区三区日本视频|