`
umbrellall1
  • 浏览: 147001 次
  • 性别: Icon_minigender_1
  • 来自: 成都
文章分类
社区版块
存档分类
最新评论

第三章:交互式通信服务与手机控制(发送Email)

阅读更多
效果:









发送成功




(注:发送前将自己手机或手机模拟器设置好信箱)
设置流程我用的是163的信箱。

图:









main.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="@+id/send"
android:layout_width="93px"
android:layout_height="wrap_content"
android:text="&#21457;&#36865;"
android:layout_x="111px"
android:layout_y="356px"
>
</Button>
<TextView
android:id="@+id/widget31"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="&#25910;&#20214;&#20154;:"
android:textSize="19sp"
android:layout_x="28px"
android:layout_y="31px"
>
</TextView>
<EditText
android:id="@+id/receiving"
android:layout_width="179px"
android:layout_height="wrap_content"
android:text=" "
android:textSize="18sp"
android:layout_x="109px"
android:layout_y="19px"
>
</EditText>
<TextView
android:id="@+id/widget33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="&#21457;&#20214;&#20154;:"
android:textSize="19sp"
android:layout_x="45px"
android:layout_y="81px"
>
</TextView>
<EditText
android:id="@+id/accessories"
android:layout_width="182px"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_x="106px"
android:layout_y="68px"
>
</EditText>
<TextView
android:id="@+id/widget35"
android:layout_width="wrap_content"
android:layout_height="26px"
android:text="&#26631;&#39064;:"
android:textSize="19sp"
android:layout_x="50px"
android:layout_y="130px"
>
</TextView>
<EditText
android:id="@+id/title"
android:layout_width="181px"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_x="105px"
android:layout_y="119px"
>
</EditText>
<EditText
android:id="@+id/text"
android:layout_width="296px"
android:layout_height="146px"
android:textSize="18sp"
android:layout_x="11px"
android:layout_y="200px"
>
</EditText>
<TextView
android:id="@+id/widget38"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="&#27491;&#25991;"
android:textSize="19sp"
android:layout_x="53px"
android:layout_y="170px"
>
</TextView>
</AbsoluteLayout>



package send.email.test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

public class SendEmailTest extends Activity {
	private EditText receiving;
	private EditText accessories;
	private EditText title;
	private EditText text;
	private Button button;
	private String[] reciver;
	private String[] subject;
	private String emailCc;
	private String emailBody;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /** 载入main.xml*/
        setContentView(R.layout.main);
        /**通过id找到EditText组件*/
        receiving =(EditText)findViewById(R.id.receiving);
        accessories=(EditText)findViewById(R.id.accessories);
        title=(EditText)findViewById(R.id.title);
        text=(EditText)findViewById(R.id.text);
        button=(Button)findViewById(R.id.send);
        /**设置Button按钮点击事件*/
        receiving.setOnKeyListener(new EditText.OnKeyListener() {
        	public boolean onKey(View v,int keyCode,KeyEvent event){
        		/**Email格式是否正确*/
        		if(isEmail(receiving.getText().toString())){
        			/**设置Button按钮为可点击*/
        			button.setEnabled(true);
        		}else{
        			/**设置Button按钮不可点击*/
        			button.setEnabled(false);
        		}
				return false;
        	}
		});
        
        
        button.setOnClickListener(new Button.OnClickListener(){
        	public void onClick(View v){
        		/**通过Intent发送Email*/
        		Intent intent=new Intent(android.content.Intent.ACTION_SEND);
        		/**
        		 * intent.setType("text/plain"); 模拟器请使用这行
				 * intent.setType("message/rfc822") ;  真机上使用这行
				 */
        		intent.setType("plain/text");
        		/**取得收件人地址 主旨正文*/
        		reciver=new String[]{
        				receiving.getText().toString()
        		};
        		subject=new String[]{accessories.getText().toString()};
        		emailCc=title.getText().toString();
        		emailBody=text.getText().toString();
        		/**将字符串放进intent*/
        		intent.putExtra(android.content.Intent.EXTRA_EMAIL, reciver);
        		intent.putExtra(android.content.Intent.EXTRA_CC, subject);
        		intent.putExtra(android.content.Intent.EXTRA_SUBJECT, emailCc);
        		intent.putExtra(android.content.Intent.EXTRA_TEXT, emailBody);
        		/**打开Email将参数传入*/
        		startActivity(Intent.createChooser(intent, getResources().getString(R.string.send)));
        	}
        });
    }
    /**匹配Email格式是否正确*/
    public static boolean isEmail(String email){
    	String strPattern="\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";      
    	Pattern pattern=Pattern.compile(strPattern);
    	Matcher matcher=pattern.matcher(email);
    	return matcher.find();
    }
}




  • 大小: 21.2 KB
  • 大小: 24.8 KB
  • 大小: 4.2 KB
  • 大小: 34.7 KB
  • 大小: 30.3 KB
分享到:
评论

相关推荐

    Web 式样书,Log4J,发送Email

    在Web应用中,通常使用Servlet API或第三方库如Apache Commons FileUpload来处理文件上传。这涉及到接收请求、解析表单数据、保存上传文件到服务器以及处理可能的错误和异常。 【项目描述】 本项目中的客户管理...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    第5章 交互式通信服务与手机控制 5.1 具有正则表达式的TextView——Linkify规则 5.2 ACTION!CALL!拨打电话——Intent.ACTION.CALL的使用 5.3 自制发送短信程序——SmsManager与PendingIntent对象 5.4 自制发送...

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    第5章 交互式通信服务与手机控制 5.1 具有正则表达式的TextView——Linkify规则 5.2 ACTION!CALL!拨打电话——Intent.ACTION.CALL的使用 5.3 自制发送短信程序——SmsManager与PendingIntent对象 5.4 自制发送...

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    第5章 交互式通信服务与手机控制 5.1 具有正则表达式的TextView——Linkify规则 5.2 ACTION!CALL!拨打电话——Intent.ACTION.CALL的使用 5.3 自制发送短信程序——SmsManager与PendingIntent对象 5.4 自制发送...

    第01章【HTML5简介】-第01节:Internet和万维网

    - **Canvas**:提供了图形绘制功能,可以实现动态和交互式的2D图形。 - **SVG**:支持可缩放矢量图形,适合用于图标和复杂的图形设计。 - **Web Workers和Web Sockets**:提高了网站的性能和实时性,前者用于后台...

    flex与j2ee(jsp sevlet)进入交互.doc

    Flex 是 Adobe 推出的一种用于创建动态、交互式图形用户界面的技术,它基于 ActionScript 语言和 MXML 格式进行开发。而 J2EE(Java 2 Platform, Enterprise Edition)则是一个用于构建企业级应用的平台,包含诸如 ...

    现代通信系统_使用MATLAB (刘树棠译).pdf

    MATLAB是一种高级编程语言和交互式环境,广泛应用于科学研究、工程计算、数据分析等领域。在通信系统的研究、设计和仿真中,MATLAB提供了强大的工具箱和函数库,能够帮助工程师和研究人员快速构建模型、分析性能、...

    控制第三方EtherCAT伺服-20200226.zip

    在本文中,我们将深入探讨如何使用TwinCat3软件来控制第三方的EtherCAT伺服驱动器,如松下和禾川的产品。 首先,TwinCat3是Beckhoff公司开发的一种集成式自动化软件,它提供了全面的编程环境和工程工具,支持PLC、...

    Google Android SDK开发范例大全的目录

    第5章 交互式通信服务与手机控制-p155 5.1 具有正则表达式的TextView——Linkify规则 5.2 ACTION!CALL!拨打电话——Intent.ACTION.CALL的使用 5.3 自制发送短信程序——SmsManager与PendingIntent对象 -p160 5.4 ...

    Google+Android+SDK开发范例大全

    第5章 交互式通信服务与手机控制 5.1 具有正则表达式的TextView——Linkify规则 5.2 ACTION!CALL!拨打电话——Intent.ACTION.CALL的使用 5.3 自制发送短信程序——SmsManager与PendingIntent对象 5.4 自制发送...

    Google Android SDK开发范例大全(完整版附部分源码).pdf

    第5章 交互式通信服务与手机控制 5.1 具有正则表达式的TextView——Linkify规则 5.2 ACTION!CALL!拨打电话——Intent.ACTION.CALL的使用 5.3 自制发送短信程序——SmsManager与PendingIntent对象 5.4 自制发送...

    Flash AS3获取PHP数据(ActionScript3 + php + email)送ActionScript2

    ActionScript3是Adobe Flash Player支持的一种编程语言,常用于创建交互式富媒体内容。PHP是一种广泛使用的服务器端脚本语言,尤其适合Web开发,可以处理各种数据,包括发送电子邮件。这里提到的“email”暗示了在AS...

    Google Android sdk 开发范例大全 部分章节代码

    第5章 交互式通信服务与手机控制-p155 5.1 具有正则表达式的TextView——Linkify规则 5.2 ACTION!CALL!拨打电话——Intent.ACTION.CALL的使用 5.3 自制发送短信程序——SmsManager与PendingIntent对象 -p160 5.4 ...

    Google Android SDK开发范例大全(完整版)

    第5章 交互式通信服务与手机控制 5.1 具有正则表达式的TextView——Linkify规则 5.2 ACTION!CALL!拨打电话——Intent.ACTION.CALL的使用 5.3 自制发送短信程序——SmsManager与PendingIntent对象 5.4 自制发送...

    Google Android SDK 开发范例大全01

    第5章 交互式通信服务与手机控制-p155 5.1 具有正则表达式的TextView——Linkify规则 5.2 ACTION!CALL!拨打电话——Intent.ACTION.CALL的使用 5.3 自制发送短信程序——SmsManager与PendingIntent对象 -p160 5.4 ...

    Google Android SDK 开发范例大全02

    第5章 交互式通信服务与手机控制-p155 5.1 具有正则表达式的TextView——Linkify规则 5.2 ACTION!CALL!拨打电话——Intent.ACTION.CALL的使用 5.3 自制发送短信程序——SmsManager与PendingIntent对象 -p160 5.4 ...

    c# 邮件客户端Email_Client

    本项目“c# 邮件客户端Email_Client”是利用C#语言实现的一个邮件客户端程序,它允许用户收发电子邮件,是理解C#与网络通信交互的重要示例。 在构建邮件客户端时,主要涉及以下核心知识点: 1. **SMTP(Simple ...

Global site tag (gtag.js) - Google Analytics