300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 基于Android的手机邮件收发(JavaMail)之二( Welcome.java 和 ReceiveAndSend.java )

基于Android的手机邮件收发(JavaMail)之二( Welcome.java 和 ReceiveAndSend.java )

时间:2024-02-13 05:38:03

相关推荐

基于Android的手机邮件收发(JavaMail)之二( Welcome.java 和 ReceiveAndSend.java )

周末休息,这次我们继上次内容继续。上一篇内容我们讲述的是一些准备工作。下载两个javamail.jar和activation.jar文件,然后再BuildPath~

言归正传,为了展示效果,在这里我申请了一个实验的新浪邮箱。

用户名:javamail_android@密 码: java_android

接下来我们就每一个.class文件分类介绍,以及每一个XXXXXactivity.class对应的layout布局文件(.xml)

1.Welcome.java

1.1 界面图如下:

1.2layout布局代码(welcome.xml)如下:

View Code

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

<View

android:layout_width="wrap_content"

android:layout_height="200px" />

<EditText

android:id="@+id/txtEmailAddress"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Email Address" />

<EditText

android:id="@+id/txtPWD"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Password"

android:inputType="textPassword" />

<requestFocus />

<View

android:id="@+id/view"

android:layout_width="wrap_content"

android:layout_height="50px" />

<Button

android:id="@+id/btnOK"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="登录邮箱" />

</LinearLayout>

1.3welcome.java代码,界面中有一个“登录邮箱”的按钮,在这里我想要做的效果是点击按钮,可以将两个EditText中的内容保存起来,用sharedPreference来做出效果。

package mi.email.activity;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import .R;

import android.app.Activity;

import android.app.Dialog;

import android.app.ProgressDialog;

import android.content.Context;

import android.content.Intent;

import android.content.SharedPreferences;

import android.content.SharedPreferences.Editor;

import android.os.Bundle;

import android.util.Log;

import android.view.Gravity;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class Welcome extends Activity {

private EditText txtEmailAddress;

private EditText txtPWD;

private Button btnOK;

private Button btnRead;

private static final String SAVE_INFORMATION = "save_information";

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.welcome);

txtEmailAddress = (EditText) findViewById(R.id.txtEmailAddress);

txtPWD = (EditText) findViewById(R.id.txtPWD);

btnOK = (Button) findViewById(R.id.btnOK);

// 给EditText进行 初始化付值,以方便运行程序

txtEmailAddress.setText("javamail_android@");

txtPWD.setText("javamail_android");

btnOK.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// 获得编辑器

SharedPreferences.Editor editor = getSharedPreferences(SAVE_INFORMATION, MODE_WORLD_WRITEABLE).edit();

// 将EditText文本内容添加到编辑器

editor.putString("save", txtEmailAddress.getText().toString() + ";" + txtPWD.getText().toString());

// 提交编辑器内容

mit();

// 定义Intent,实现点击按钮,进行界面跳转

Intent intent = new Intent();

intent.setClass(Welcome.this, ReceiveAndSend.class);

startActivity(intent);

}

});

}

}

2.ReceiveAndSend.java

2.1我们通过名字不难看出它的功能是:收邮件以及发送邮件。这里用点击按钮的方式来实现效果。

界面比较简单,在这就不弄布局代码了.

2.2ReceiveAndSend.java ,功能较为简单,按钮触发事件,登录到另外所需要的界面。(收邮件和发邮件)

package mi.email.activity;

import .R;

import android.app.Activity;

import android.app.Dialog;

import android.app.ProgressDialog;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class ReceiveAndSend extends Activity {

private Button btnReceiveEmail;

private Button btnSendEmail;

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.receive_send);

btnReceiveEmail = (Button) findViewById(R.id.btnReceiveEmail);

btnSendEmail = (Button) findViewById(R.id.btnSendEmail);

btnReceiveEmail.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// TODO Auto-generated method stub

Intent intent_second = new Intent();

intent_second.setClass(ReceiveAndSend.this, ReceiveList.class);

showDialog(0);//显示进度条对话框

startActivity(intent_second);

}

});

btnSendEmail.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// TODO Auto-generated method stub

Intent intent_third=new Intent();

intent_third.setClass(ReceiveAndSend.this, SendMail.class);

showDialog(0);//显示进度条对话框'

startActivity(intent_third);

}

});

}

protected Dialog onCreateDialog(int id) {// 显示网络连接Dialog

ProgressDialog dialog = new ProgressDialog(this);

dialog.setTitle("请稍候。。。");

dialog.setIndeterminate(true);

dialog.setMessage("程序正在加载。。。");

return dialog;

}

}

Ps:在下面的两篇文章中,介绍基于javamail的邮件接收和发送。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。