Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 技術基礎

第75章、再識 Intent — 呼叫系統寄送 Email(從零開始學 Android)

編輯:Android 技術基礎

利用 Intent 寄送郵件,其本質其實是「呼叫」Android 系統內建的郵件程式來完成發送,並非自行實作一套郵件傳輸機制。透過 ACTION_SEND 這個動作,再搭配收件人、主題與內文等額外資料,就能把內容交給系統的郵件應用處理。

一、設計介面

1. 版面配置

開啟 res/layout/activity_main.xml,加入收件人、主題、內容輸入框以及發送按鈕:

[html] view plain copy  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout   
  3.     xmlns:android="https://schemas.android.com/apk/res/android"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.         android:id="@+id/textView1"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_alignParentLeft="true"  
  13.         android:layout_alignParentTop="true"  
  14.         android:text="收件人:" />  
  15.   
  16.     <EditText  
  17.         android:id="@+id/recieve"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:ems="10" >  
  21.   
  22.         <requestFocus />  
  23.     </EditText>  
  24.       
  25.      <TextView  
  26.         android:id="@+id/textView2"  
  27.         android:layout_width="wrap_content"  
  28.         android:layout_height="wrap_content"  
  29.         android:text="主題" />  
  30.   
  31.     <EditText  
  32.         android:id="@+id/subject"  
  33.         android:layout_width="wrap_content"  
  34.         android:layout_height="wrap_content"  
  35.         android:ems="10" />  
  36.       
  37.   
  38.     <TextView  
  39.         android:id="@+id/textView3"  
  40.         android:layout_width="wrap_content"  
  41.         android:layout_height="wrap_content"  
  42.         android:text="內容" />  
  43.   
  44.     <EditText  
  45.         android:id="@+id/content"  
  46.         android:layout_width="wrap_content"  
  47.         android:layout_height="wrap_content"  
  48.         android:ems="10" />  
  49.   
  50.     <Button  
  51.         android:id="@+id/send"  
  52.         android:layout_width="wrap_content"  
  53.         android:layout_height="wrap_content"  
  54.         android:text="發送郵件" />  
  55.   
  56. </LinearLayout>  

二、程式碼實作

開啟 src/com.genwoxue.intentemail/MainActivity.java,加入以下程式碼:

[java] view plain copy  
  1. package com.genwoxue.intentemail;  
  2.   
  3.   
  4. import android.os.Bundle;  
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.TextView;  
  11.   
  12. public class MainActivity extends Activity {  
  13.   
  14.     private TextView tvRecieve=null;  
  15.     private TextView tvSubject=null;  
  16.     private TextView tvContent=null;  
  17.     private Button btnSend=null;  
  18.     @Override  
  19.      public void onCreate(Bundle savedInstanceState)     
  20.     {     
  21.         super.onCreate(savedInstanceState);                
  22.         setContentView(R.layout.activity_main);  
  23.           
  24.         tvRecieve=(TextView)super.findViewById(R.id.recieve);   //收件人  
  25.         tvSubject=(TextView)super.findViewById(R.id.subject);   //主題  
  26.         tvContent=(TextView)super.findViewById(R.id.content);   //內容  
  27.           
  28.         btnSend=(Button)super.findViewById(R.id.send);  
  29.         //發送郵件Email  
  30.         btnSend.setOnClickListener(new OnClickListener(){  
  31.             public void onClick(View v)  
  32.             {    
  33.                 String recieve[]=new String[1];  
  34.                 recieve[0]=tvRecieve.getText().toString();      //獲取收件人  
  35.                 String subject=tvSubject.getText().toString();  //獲取主題  
  36.                 String content=tvContent.getText().toString();  //攻取內容  
  37.                   
  38.                 Intent intent=new Intent(Intent.ACTION_SEND);   //發送郵件使用ACTION_SEND  
  39.                 intent.setType("plain/text");                   //設置類型  
  40.                   
  41.                 //調用系統發送郵件  
  42.                 intent.putExtra(Intent.EXTRA_EMAIL,recieve);  
  43.                 intent.putExtra(Intent.EXTRA_SUBJECT,subject);  
  44.                 intent.putExtra(Intent.EXTRA_TEXT,content);  
  45.                 MainActivity.this.startActivity(intent);  
  46.             }  
  47.         });  
  48.     }  
  49.         
  50. }             

三、執行結果

 

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved