最近需要发送彩信功能,以下是整理封装的代码。
1. 发送调用函数:
/*
* 传入彩信配置信息,发送彩信
*/
private void sendMMS(String imgPath, String text, String phoneNO, String subject)
{
//验证
if(!isValidPhoneNumber(phoneNO))
{
throw new IllegalArgumentException("无效的手机号码");
}
if(null == _text)
{
_text = "展品信息为空";
}
//添加图片 "file://E:/photojpg/a01.jpg"
String mimeTypeImage = "image/jpeg";
mmsmsg.addImagePart(imgPath, mimeTypeImage);
//添加文本
String mimeTypeText = "text/plain";
String encoding = "UTF-8";
mmsmsg.addTextPart(text, mimeTypeText, encoding);
//发送彩信
String appID = "com.nakia.nfc.sample.app";
SendMMS sendMMS = new SendMMS(mmsmsg.getParts(), appID, phoneNO, subject);
sendMMS.start();
}
/**
* 验证手机号码是否为数字
*/
private boolean isValidPhoneNumber(String phoneNO)
{
char[] chars = phoneNO.toCharArray();
if (chars.length == 0) {
return false;
}
int startPos = 0;
// initial '+' is OK
if (chars[0] == '+') {
startPos = 1;
}
for (int i = startPos; i < chars.length; ++i) {
if (!Character.isDigit(chars[i])) {
return false;
}
}
return true;
}
2. MMSMessage类:
package com.nokia.nfc.sample.app;
import java.io.InputStream;
import javax.wireless.messaging.*;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
/**
*
* 添加彩信内容,包括添加文本和图片两个方法
*
*/
public class MMSMessage
{
private Vector parts = new Vector();
private int counter = 0;
/*
* 获得添加完成后的彩信内容
*/
public MessagePart[] getParts()
{
// parts 是一个Vector对象,它包括Mulitpart 消息
MessagePart[] partsArray = new MessagePart[parts.size()];
parts.copyInto(partsArray);
return partsArray;
}
/*
* 添加文本部分到彩信
*/
public void addTextPart(String text, String mimeType, String encoding)
{
MessagePart mpart = null;
byte[] contents = null;
try
{
contents = text.getBytes(encoding);
mpart = new MessagePart(contents, 0, contents.length, mimeType, "id" +
counter, "contentLocation", encoding);
parts.addElement(mpart);
counter ++;
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
mimeType = null;
encoding = null;
mpart = null;
contents = null;
}
}
/*
*添加图片部分到彩信
*imgPath = "file://E:/photojpg/a01.jpg";
*/
public void addImagePart(String imgPath, String mimeType)
{
MessagePart mpart = null;
FileConnection fconn = null;
InputStream is = null;
byte[] contents = null;
try
{
//String mimeType = "image/jpeg";
fconn = (FileConnection)Connector.open(imgPath,Connector.READ);
is = fconn.openInputStream();
contents = new byte[is.available()];
is.read(contents);
mpart = new MessagePart(contents, 0, contents.length,mimeType, "id" +
counter,"contentLocation", null);
parts.addElement(mpart);
counter ++;
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
mpart = null;
contents = null;
}
}
}
3. SendMMS 类
package com.nokia.nfc.sample.app;
import javax.wireless.messaging.*;
import java.io.IOException;
import javax.microedition.io.Connector;
/**
* 发送彩信
*/
public class SendMMS extends Thread
{
private MessagePart[] _msgParts = null;
// 从jad文件中获得:getAppProperty("MMS-ApplicationID");
private String _appID = "com.nakia.nfc.sample.app";
private String _phoneNO = null;
private String _address = null;
// private String _subject = null;
public SendMMS(MessagePart[] msgParts, String appID, String phoneNO, String subject)
{
this._msgParts = msgParts;
this._appID = appID;
this._phoneNO = phoneNO;
// this._subject = subject;
}
/*
* 发送彩信
*/
public void run()
{
MessageConnection mmsconn = null;
try
{
_address = "mms://"+ _phoneNO+":" + _appID;
/** 打开连接 */
mmsconn = (MessageConnection) Connector.open(_address);
MultipartMessage mmmessage =(MultipartMessage) mmsconn.newMessage(
MessageConnection.MULTIPART_MESSAGE);
mmmessage.setAddress(_address);
for (int i = 0; i < _msgParts.length; i++)
{
mmmessage.addMessagePart(_msgParts[i]);
}
// 由于J2me彩信设置主题只有setSubject(String)一个方法,而其内部默认字符编码格式
// 为UTF-16,所以中文主题显示为乱码,故不显示主题
// byte[] subjectBytes = _subject.getBytes("UTF-8");
// String tempSubject = new String(subjectBytes, "UTF-8");
// mmmessage.setSubject(_subject);
mmsconn.send(mmmessage);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (mmsconn != null)
{
try
{
mmsconn.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
}
}