Android中调用邮件发送程序
分类:Android
阅读 (2,133)
Add comments
8月 092012
本例主要是使用了Intent对象的隐式调用方式,设置intent的Action, Data和Extra来传递的相关的信息
activity布局代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<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" > <EditText android:id="@+id/txtReceiver" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/textView1" android:ems="10" android:inputType="textEmailAddress" android:text="yourmail@svn1.bcoder.com" /> <EditText android:id="@+id/txtEmailContent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/txtReceiver" android:ems="10" android:gravity="top" android:inputType="textMultiLine" android:lines="10" android:text="Here is the bodytext!" /> <Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/txtEmailContent" android:layout_marginRight="31dp" android:layout_marginTop="16dp" android:onClick="callEmailApp" android:text="Call" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/txtReceiver" android:layout_alignBottom="@+id/txtReceiver" android:layout_alignParentLeft="true" android:text="Receiver:" android:textAppearance="?android:attr/textAppearanceMedium" /> </RelativeLayout> |
类代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.bcoder.callmailapp; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; import android.support.v4.app.NavUtils; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } public void callEmailApp(View view){ EditText txtReceiver = (EditText)findViewById(R.id.txtReceiver); EditText txtContent = (EditText)findViewById(R.id.txtEmailContent); Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("mailto:" + txtReceiver.getText().toString())); intent.putExtra(Intent.EXTRA_SUBJECT, "Welcome to bcoder.com"); intent.putExtra(Intent.EXTRA_BCC, "bcc@svn1.bcoder.com"); intent.putExtra(Intent.EXTRA_CC, "cc@svn1.bcoder.com"); intent.putExtra(Intent.EXTRA_TEXT, txtContent.getText().toString()); intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"coder1@svn1.bcoder.com", "coder2@svn1.bcoder.com"}); startActivity(intent); } } |
本代码调试环境:
* Eclipse: Indigo Service Release 2
* Android SDK: 4.0(API LEVEL 14)
注意:
1. 如果你还没有在你的手机或者AVD上创建相关的email帐户,运行此程序时会提示Unsupported action(不支持的Action)!