技术开发 频道

OPhone学习笔记 - Mail API 的使用

  二、主界面介绍

  public class MailAPIDemoActivity extends Activity implements POP3ProcessListener

  该程序应用了POP3ProcessListener的接口,具有java基础的朋友们对此应该不会陌生。该接口要求实现void receivedMailMessage(BaseAccountInfo accountInfo, CMMail m)函数,作为收到邮件消息的响应。

  该函数内容为: if (mail != null) printLog("Received new mail: \n" + mail.toString());

  还有若干变量常量的定义:

  // 这里定义了帐号的基本信息,是OPhone自带的邮件功能类

  public BaseAccountInfo accountInfo;

  // 该Listener可以关注邮件任务状态

  public MailEventListenerClass mailEventListener;

  public View mainView;

  public static final String TAG = "MailExample";

  public static final String TAG_CMMail = "CMMail";

  public static final String TAG_POP3ProcessListener = "POP3ProcessListener: ";

  接下来是onCreate代码:

  在主界面首先定义了mainView和textView,此外,还有邮件消息的Listener和帐号信息。

  接下来,定义了三个按钮,一个是配置邮件,一个是接收邮件,一个是发送邮件。

  1.配置邮件

  Button configButton = (Button) findViewById(R.id.button_config_account);

  configButton.setOnClickListener(new Button.OnClickListener() {

  public void onClick(View v) {

  ConfigAccountLayout view = (ConfigAccountLayout) ConfigAccountLayout.inflate(

  MailAPIDemoActivity.this, R.layout.config_account, null);

  view.setAccountInfo(accountInfo);

  setContentView(view);

  }

  });

  从上面代码我们可以看到,按钮代码主要流程是:按钮定义、建立Listener、配置单击事件、设置事件响应内容(如切换到ConfigAccountLayout)。在新建ConfigAccountLayout时,我们用到了inflate函数,它可以从一个已有的资源扩展生成一个新的view该函数定义如下:

  View android.view.View.inflate(Context context, int resource, ViewGroup root)

  setAccountInfo函数是在ConfigAccountLayout.java中定义的,用于设置账户信息,在我们会在后面进行分析。最后将当前的视图切换至配置邮件视图。

  2.接收邮件

  Button popbutton = (Button) findViewById(R.id.button_pop);

  popbutton.setOnClickListener(new Button.OnClickListener() {

  public void onClick(View v) {

  if (accountInfo == null) {

  printLog("Please config account first!");

  return;

  }

  printLog("Start fetch new mail...");

  // 获取服务器邮件消息

  CMMailTaskController.getInstance().startFetchNewMessages(MailAPIDemoActivity.this,accountInfo,null, mailEventListener,MailAPIDemoActivity.this);

  }

  });

  接收邮件的功能实现较容易理解,首先检查账户配置,然后通过收邮件函数执行功能。

  3.发送邮件

  Button smtpbutton = (Button) findViewById(R.id.button_smtp);

  smtpbutton.setOnClickListener(new Button.OnClickListener() {

  public void onClick(View v) {

  if (accountInfo == null) {

  printLog("Please config account first!");

  return;

  }

  try {

  // 新建邮件实例

  CMMail mail = new CMMail();

  //添加发送地址

  EditText inputTo = (EditText)MailAPIDemoActivity.this.findViewById(R.id.input_to);

  String toAddress = inputTo.getText().toString();

  if (toAddress == null || toAddress.length() == 0) {

  printLog("Please input address first");

  return;

  }

  CMMailAddress toadd = new CMMailAddress(null, toAddress);

  mail.addToAddress(toadd);

  // 设置邮件主题

  mail.setSubject("OMS API Test Mail");

  //设置邮件内容

  mail.setBodyTxt("Hello!\nThis is a mail from OMS API Tester!");

  // 创建发送列表

  CMMail[] sendmails = new CMMail[] { mail };

  // 开始发送消息

  CMMailTaskController.getInstance().startSendMessages(MailAPIDemoActivity.this,accountInfo, sendmails,mailEventListener);

  printLog("Posted mail to: " + toadd.getAddress()+ "; Subject: " + mail.getSubject());

  } catch (Exception e) {

  printLog(TAG_POP3ProcessListener + e.toString());

  Log.d(TAG, TAG_POP3ProcessListener + e.toString());

  }

  }

  });

  }

  该主界面还包含了一个class,我们来分析邮件状态响应:

  public class MailEventListenerClass implements MailEventListener

  它需要实现 MailEventListener接口,该接口函数内容如下:

  事件分为三类:登录、收取邮件、收取带附件邮件,具体事件的列表描述可以查看:http://www.ophonesdn.com/documentation/ophone/reference/ophoneapi/mail/oms/mail/MailBaseEvent.html

  在主程序的最后,还有两个函数,实现了打印日志的功能。

  putstring功能是将文本"text"插入到字符串text中。作为在消息队列中Handler提取的关键字。

  Handler可以处理和发送与消息队列相关的消息或者运行对象。handleMessage是必须实现的接口,用于接收处理消息。在该handlerlog里面,将msg显示到view中。在printlog中,sendMessage函数功能是添加消息至消息队列尾部。

0
相关文章