Intent 行为为
android.content.Intent.ACTION_SEND 。实际上在 Android 上使用的邮件发送服务是调用
Gmail程序,而非直接使用SMTP的Protocol 。现在介绍本篇需要使用到的功能清单:
验证用户输入是否为正确的邮箱格式;
用户可以先把手动输入邮箱,也可以长按邮箱文本框跳到联系人那里找到联系人,得到联系人的邮箱,后返回;发送邮件。
java代码:
1. <?xml version="1.0" encoding="utf-8"?>
2. <AbsoluteLayout
3. android:id="@+id/widget34"
4. android:layout_width="fill_parent"
5. android:layout_height="fill_parent"
6. android:background="@drawable/white"
7. xmlns:android="http://schemas.android.com/apk/res/android"
8. >
9. <TextView
10. android:id="@+id/myTextView1"
11. android:layout_width="wrap_content"
12. android:layout_height="wrap_content"
13. android:text="@string/str_receive"
14. android:layout_x="60px"
15. android:layout_y="22px"
16. >
17. </TextView>
18. <TextView
19. android:id="@+id/myTextView2"
20. android:layout_width="wrap_content"
21. android:layout_height="wrap_content"
22. android:text="@string/str_cc"
23. android:layout_x="60px"
24. android:layout_y="82px"
25. >
26. </TextView>
27. <EditText
28. android:id="@+id/myEditText1"
29. android:layout_width="fill_parent"
30. android:layout_height="wrap_content"
31. android:textSize="18sp"
32. android:layout_x="120px"
33. android:layout_y="12px"
34. >
35. </EditText>
36. <EditText
37. android:id="@+id/myEditText2"
38. android:layout_width="fill_parent"
39. android:layout_height="wrap_content"
40. android:textSize="18sp"
41. android:layout_x="120px"
42. android:layout_y="72px"
43. >
44. </EditText>
45. <Button
46. android:id="@+id/myButton1"
47. android:layout_width="wrap_content"
48. android:layout_height="124px"
49. android:text="@string/str_button"
50. android:layout_x="0px"
51. android:layout_y="2px"
52. >
53. </Button>
54. <TextView
55. android:id="@+id/myTextView3"
56. android:layout_width="wrap_content"
57. android:layout_height="wrap_content"
58. android:text="@string/str_subject"
59. android:layout_x="60px"
60. android:layout_y="142px"
61. >
62. </TextView>
63. <EditText
64. android:id="@+id/myEditText3"
65. android:layout_width="fill_parent"
66. android:layout_height="wrap_content"
67. android:textSize="18sp"
68. android:layout_x="120px"
69. android:layout_y="132px"
70. >
71. </EditText>
72. <EditText
73. android:id="@+id/myEditText4"
74. android:layout_width="fill_parent"
75. android:layout_height="209px"
76. android:textSize="18sp"
77. android:layout_x="0px"
78. android:layout_y="202px"
79. >
80. </EditText>
81. </AbsoluteLayout>
82.
复制代码
判断用户输入邮箱是否正确方法为:
java代码:
1. public static boolean isEmail(String strEmail) {
2. String strPattern = "^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
3.
4. Pattern p = Pattern.compile(strPattern);
5. Matcher m = p.matcher(strEmail);
6. return m.matches();
7. }
8.
复制代码
记得之前在MM开发社区有网友发过请教贴,请教如何从一个Activity跳转到另一个Activity后得到需要的数据后返回加载到原有的Activitiy所在的控件上,大致原理是这样的,一般程序做跳转都是用StartAcivity方法,如果想实现跳转并取值返回,就需要用到startActivityForResult(intent,requestCode),其中requestCode为一个Activity要返回值的依据,可以为任意int类型。你可以自己定义常量,也可以自己指定数字。程序覆盖了onActivityResult()这个方法,令程序收到result后,再重新加载写回原本需要加载的控件上。本例中调了文本框的长按事件,当文本框长按即自行跳转到联系人页面上,点击需要的联系人名称返回该联系人的邮箱号回到我们的主程序窗口并加载到文本上。
问题:如何找到联系人并查询它的邮箱号?这里用到的是Content Provider。
关于Content Provider
如果你要公开你的数据,你可以创建或者使用一个Content Provider。它是一个能使所以应用程度都能存储和检索数据的对象。它是唯一在包和包之间分享数据的方法;因为不存在那种供所有的包来共享的一般存储空间。Android自带了一些Content Provider,它们用于一些一般的数据类型(音频,视频,图片,个人联系信息等等)。您能从 Provider这个包中看到一些Android自带的Content Provider。到底表面之下一个Content Provider是如何存储数据的决定于这个Content Provider是如何实现的,但是所有的Content Provider必须实现一种一般公约用来数据查询和一种一般公约来返回结果。然而,一个Content Provider能够实现自定义的方法,使得在处理一些特定的数据时,对于数据的存储/检索更加简单。
用户长按文本框后,得通过 Content Provider查找跳转到联系人中心,查找用户并返回邮箱代码如下:
java代码:
1. private OnLongClickListener searhEmail=new OnLongClickListener(){
2. public boolean onLongClick(View arg0) {
3. Uri uri=Uri.parse("content://contacts/people");
4. Intent intent=new Intent(Intent.ACTION_PICK,uri);
5. startActivityForResult(intent, PICK_CONTACT_SUBACTIVITY);
6. return false;
7. }
8. ;
9. };
10.
11. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
12.
13. switch (requestCode) {
14. case PICK_CONTACT_SUBACTIVITY:
15. final Uri uriRet=data.getData();
16. if(uriRet!=null)
17. {
18. try {
19. Cursor c=managedQuery(uriRet, null, null, null, null);
20. c.moveToFirst();
21. //取得联系人的姓名
22. String strName=c.getString(c.getColumnIndexOrThrow(People.NAME));
23. //取得联系人的EMAIL
24. String[] PROJECTION=new String[]{
25. Contacts.ContactMethods._ID,
26. Contacts.ContactMethods.KIND,
27. Contacts.ContactMethods.DATA
28. };
29. //查询指定人的Email
30. Cursor newcur=managedQuery(
31. Contacts.ContactMethods.CONTENT_URI,
32. PROJECTION,
33. Contacts.ContactMethods.PERSON_ID+"=\'"
34. +c.getLong(c.getColumnIndex(People._ID))+"\'",
35. null, null);
36. startManagingCursor(newcur);
37. String email="";
38. if(newcur.moveToFirst())
39. {
40. email=newcur.getString(newcur.getColumnIndex
41. (Contacts.ContactMethods.DATA));
42. myEditText.setText(email);
43. }
44.
45. } catch (Exception e) {
46. // TODO: handle exception
47. Toast.makeText(sendEmailActivity.this, e.toString(), 1000).show();
48. }
49. }
50. break;
51.
52. default:
53. break;
54. }
55. super.onActivityResult(requestCode, resultCode, data);
56. };
57.
复制代码
注意使用Content Provider查找联系必须在配置文件里面配置权限,具体权限如下:<uses-permission android:name=”android.permission.READ_CONTACTS”/>
邮件发送程序并不复杂,主要是在 EditText 、Button 控件的构建,通过构造一个自定义的 Intent(android.content.Intent.ACTION_SEND)作为传送 Email 的 Activity 之用,在该Intent中,还必须使用 setType()来决定 Email的格式,使用 putExtra() 来置入寄件入(EXTRA_EMAIL)、主题(EXTRA_SUBJECT)、邮件内容(EXTRA_TEXT)以及其他Email的字段(EXTRA_BCC、EXTRA_CC)。代码如下:
java代码:
1.
2. myButton.setOnClickListener(new OnClickListener() {
3.
4. @Override
5. public void onClick(View v) {
6. // TODO Auto-generated method stub
7. Intent mailIntent=new Intent(android.content.Intent.ACTION_SEND);
8. mailIntent.setType("plain/test");
9. strEmailReciver=new String[]{ myEditText.getText().toString() };
10. strEmailCC=new String[]{myEditText2.getText().toString()};
11. strEmailSubject=myEditText3.getText().toString();
12. strEmailBody=myEditText4.getText().toString();
13.
14.
15. mailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, strEmailReciver);
16. mailIntent.putExtra(android.content.Intent.EXTRA_CC, strEmailCC);
17. mailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, strEmailSubject);
18. mailIntent.putExtra(android.content.Intent.EXTRA_TEXT, strEmailBody);
19. startActivity(Intent.createChooser(mailIntent, getResources().getString(R.string.send)));
20. }
21. });