作者:hunhun1981
出自:http://blog.csdn.net/hunhun1981/
这个是彩信的测试程序,跟上一个短信测试的代码类似,但只做了文字,不包含图片。仅仅是为了测试彩信发送功能。
import
javax.microedition.io.Connector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.MessagePart;
import javax.wireless.messaging.MultipartMessage;
public class MMSMIDlet extends MIDlet implements CommandListener {
private Command exit;
private Command send;
private TextField number;
private TextField content;
private Form form;
public MMSMIDlet() {
number = new TextField( " Phone Number: " , " 13488755864 " , 50 ,
TextField.ANY);
content = new TextField( " Content: " , " mms test " , 256 , TextField.ANY);
send = new Command( " send " , Command.OK, 0 );
exit = new Command( " exit " , Command.BACK, 1 );
form = new Form( " MMS Test " );
form.append(number);
form.append(content);
form.addCommand(send);
form.addCommand(exit);
form.setCommandListener( this );
}
public void commandAction(Command c, Displayable d) {
if (d.equals(form)) {
if (c.equals(send)) {
String num = number.getString();
String con = content.getString();
if (num != null && num.length() > 1 ) {
if (con != null && con.length() > 1 ) {
sendMMS(num, con);
}
}
} else if (c.equals(exit)) {
notifyDestroyed();
}
}
}
protected void destroyApp( boolean arg0) {
}
protected void pauseApp() {
}
protected void startApp() {
Display.getDisplay( this ).setCurrent(form);
}
public void sendMMS( final String phoneNumber, final String message) {
new Thread() {
public void run() {
MessagePart mpart = null ;
String mimeType = " text/plain " ;
String encoding = " GB2312 " ;
byte [] contents = null ;
try {
String address = " mms:// " + phoneNumber;
contents = message.getBytes(encoding);
mpart = new MessagePart(contents, 0 , contents.length,
mimeType, " id " + Integer.toString( 1 ),
" contentLocation " , encoding);
form.append( " 创建信息元 " );
MessageConnection mmsconn = (MessageConnection) Connector.open(address);
form.append( " 建立连接 " );
MultipartMessage mmmessage = (MultipartMessage) mmsconn
.newMessage(MessageConnection.MULTIPART_MESSAGE);
form.append( " 创建信息 " );
mmmessage.setAddress(address);
mmmessage.addMessagePart(mpart);
mmmessage.setSubject( " MMS Text " );
form.append( " 设置信息 " );
mmsconn.send(mmmessage);
form.append( " 发送完成 " );
mmsconn.close();
form.append( " 关闭连接 " );
} catch (Exception e) {
form.append(e.toString());
}
}
}.start();
}
}
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.MessagePart;
import javax.wireless.messaging.MultipartMessage;
public class MMSMIDlet extends MIDlet implements CommandListener {
private Command exit;
private Command send;
private TextField number;
private TextField content;
private Form form;
public MMSMIDlet() {
number = new TextField( " Phone Number: " , " 13488755864 " , 50 ,
TextField.ANY);
content = new TextField( " Content: " , " mms test " , 256 , TextField.ANY);
send = new Command( " send " , Command.OK, 0 );
exit = new Command( " exit " , Command.BACK, 1 );
form = new Form( " MMS Test " );
form.append(number);
form.append(content);
form.addCommand(send);
form.addCommand(exit);
form.setCommandListener( this );
}
public void commandAction(Command c, Displayable d) {
if (d.equals(form)) {
if (c.equals(send)) {
String num = number.getString();
String con = content.getString();
if (num != null && num.length() > 1 ) {
if (con != null && con.length() > 1 ) {
sendMMS(num, con);
}
}
} else if (c.equals(exit)) {
notifyDestroyed();
}
}
}
protected void destroyApp( boolean arg0) {
}
protected void pauseApp() {
}
protected void startApp() {
Display.getDisplay( this ).setCurrent(form);
}
public void sendMMS( final String phoneNumber, final String message) {
new Thread() {
public void run() {
MessagePart mpart = null ;
String mimeType = " text/plain " ;
String encoding = " GB2312 " ;
byte [] contents = null ;
try {
String address = " mms:// " + phoneNumber;
contents = message.getBytes(encoding);
mpart = new MessagePart(contents, 0 , contents.length,
mimeType, " id " + Integer.toString( 1 ),
" contentLocation " , encoding);
form.append( " 创建信息元 " );
MessageConnection mmsconn = (MessageConnection) Connector.open(address);
form.append( " 建立连接 " );
MultipartMessage mmmessage = (MultipartMessage) mmsconn
.newMessage(MessageConnection.MULTIPART_MESSAGE);
form.append( " 创建信息 " );
mmmessage.setAddress(address);
mmmessage.addMessagePart(mpart);
mmmessage.setSubject( " MMS Text " );
form.append( " 设置信息 " );
mmsconn.send(mmmessage);
form.append( " 发送完成 " );
mmsconn.close();
form.append( " 关闭连接 " );
} catch (Exception e) {
form.append(e.toString());
}
}
}.start();
}
}
更多信息,请关注hunhun1981的专栏 。