J2ME 发送彩信代码

最近需要发送彩信功能,以下是整理封装的代码。

 

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();
                }
            }
        }
}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

L_serein

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值