j2me实现收发短信

在nokia 6600上测试可发送短信^-^

 

public class sendthread extends thread {
    private string phone;

 

    private string content;

 

    public sendthread(string phone, string content) {
        this.phone = phone;
        this.content = content;
    }

 

    public void run() {
        try {
            string addr = "sms://" + phone;
            //system.out.println("发送地址为:" + addr);
            messageconnection conn = (messageconnection) connector.open(addr);
            textmessage msg = (textmessage) conn.newmessage(messageconnection.text_message);
            msg.setpayloadtext(content);
            conn.send(msg);
            conn.close();
        } catch (exception e) {
            //system.out.println("error in sending");
            e.printstacktrace();
        }
    }
}

 

第二种j2me发送短信的方法,nokia 6600实机测试通过:-)

 

import javax.microedition.io.connector;
import javax.microedition.lcdui.alert;
import javax.microedition.lcdui.alerttype;
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.microedition.midlet.midletstatechangeexception;
import javax.wireless.messaging.message;
import javax.wireless.messaging.messageconnection;
import javax.wireless.messaging.textmessage;

 

public class sendnotemethod2 extends midlet implements commandlistener {
    private display dis;

 

    private command send;

 

    private string serverport;

 

    private form f;

 

    private textfield tf1, tf2;

 

    private boolean done;

 

    private messageconnection sconn;

 

    public sendnotemethod2() {
        super();
        dis = display.getdisplay(this);
        f = new form("短信发送测试");
        send = new command("发送", command.ok, 0);
        tf1 = new textfield("电话:", null, 255, textfield.phonenumber);
        tf2 = new textfield("内容:", null, 255, textfield.any);
        f.append(tf1);
        f.append(tf2);
        f.addcommand(send);
        f.setcommandlistener(this);
        dis.setcurrent(f);
        serverport = "5000";
    }

 

    protected void startapp() throws midletstatechangeexception {
        try {
            sconn = (messageconnection) connector.open("sms://:" + serverport);
            done = false;
            new thread(new smsserver()).start();
        } catch (exception e) {
            system.out.print("error in start/n");
            e.printstacktrace();
        }
    }

 

    protected void pauseapp() {
        done = true;
        try {
            sconn.close();
        } catch (exception e) {
            system.out.print("error in pause");
            e.printstacktrace();
        }

 

    }

 

    protected void destroyapp(boolean arg0) throws midletstatechangeexception {
        done = true;
        try {
            sconn.close();
        } catch (exception e) {
            system.out.print("error in pause");
            e.printstacktrace();
        }
    }

 

    public void commandaction(command c, displayable d) {
        if (c == send) {
            try {

 

                thread send2thread = new thread() {
                    public void run() {
                        system.out.println("try send2command.....");

 

                        try {
                            string addr = "sms://" + tf1.getstring();
                            system.out.println("发送地址为:" + addr);
                            textmessage msg = (textmessage) sconn.newmessage(messageconnection.text_message);
                            msg.setaddress(addr);
                            msg.setpayloadtext(tf2.getstring());
                            sconn.send(msg);
                        } catch (exception exc) {
                            exc.printstacktrace();
                        }
                    }
                };
                send2thread.start();
            } catch (exception e) {
                system.out.println("error in sending");
                e.printstacktrace();
            }
        }
    }

 

    class smsserver implements runnable {
        public void run() {
            try {
                while (!done) {
                    message msg = sconn.receive();
                    if (msg instanceof textmessage) {
                        textmessage tmsg = (textmessage) msg;
                        string msgtext = tmsg.getpayloadtext();

 

                        // construct the return message
                        textmessage rmsg = (textmessage) sconn
                                .newmessage(messageconnection.text_message);
                        rmsg.setaddress(tmsg.getaddress());
                        rmsg.setpayloadtext("message " + msgtext
                                + " is received");
                        sconn.send(rmsg);

 

                        alert alert = new alert("received", msgtext, null,
                                alerttype.error);
                        alert.settimeout(alert.forever);
                        dis.setcurrent(alert);
                    } else {
                        throw new exception("received is not a text mesg");
                    }
                }
            } catch (exception e) {
                system.out.println("error in server receiving");
                e.printstacktrace();
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值