CSDN博客

img xdong001

发送短信

发表于2008/10/2 15:49:00  384人阅读

 

开个线程检测是否收到指定号码的短信,若收到则调用这个类的方法发送预先编辑好的内容给他

Java code
* 程序员: 杨星辉 * * 日 期: 2008-7-4 * * 时 间: 下午05:24:07 * * 短信发送类 */ import javax.wireless.messaging.*; import javax.microedition.io.*; public class Sender { public Sender() { } /* 传入文字内容、电话号码、端口号,进行发送短信,结果为true or false */ public static boolean send(String content, String phonenumber, String smsPort) { boolean result = true; try { String address = "sms://" + phonenumber + ":" + smsPort; MessageConnection conn = (MessageConnection) Connector.open(address); TextMessage msg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE); msg.setPayloadText(content); conn.send(msg); conn.close(); } catch (Exception e) { result = false; e.getMessage(); } return result; } /* 无端口 发送短信 */ public static boolean send(String content, String phonenumber) { boolean result = true; try { String address = "sms://" + phonenumber; MessageConnection conn = (MessageConnection) Connector.open(address); TextMessage msg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE); msg.setPayloadText(content); conn.send(msg); conn.close(); } catch (Exception e) { result = false; e.getMessage(); } return result; } }

给你一段我自己用的代码,用着没出现过问题。

Thread fetchThread = new Thread() {
                public void run() {
                    System.out.println("Try send.....");
                   
                    try { //发送
                        String addr = "sms://" + strDest;
                        System.out.println("发送地址为:" + addr);
                        MessageConnection conn = (MessageConnection) Connector
                                .open(addr);
                        TextMessage msg = (TextMessage) conn
                                .newMessage(MessageConnection.TEXT_MESSAGE);
                        msg.setPayloadText(strMesg);
                        conn.send(msg);
                        conn.close();
                       
                        //清除已写的信息                                                             
                    } catch (Exception exc) {
                        exc.printStackTrace();
                    }
                }
            };
            fetchThread.start();
           
        } catch (Exception e) {
            System.out.println("Error in sending");
            e.printStackTrace();
        }

 

 

Java串口通信-收发短信

 

import org.smslib.*;
import javax.comm.*;
import java.io.*;
import java.util.*;

public class Comcon{
    
   
public static void main(String args[]){

       
int bauds[] ={9600,19200,38400,57600,115200,230400,460800};
       
   
   
for(int i=0;i<bauds.length;i++){
        System.out.println(
"Trying at " + bauds[i] + "...");
        System.out.println(
"----------------------------------------");
   
try
       {
        CService srv
= new CService("COM3",9600, “NOKIA”, null);

        srv.connect();
        System.out.println(
"Mobile Device Information: ");
        System.out.println(
"    Manufacturer  : " + srv.getDeviceInfo().getManufacturer());
        System.out.println(
"    Model         : " + srv.getDeviceInfo().getModel());
        System.out.println(
"    Serial No     : " + srv.getDeviceInfo().getSerialNo());
        System.out.println(
"    IMSI          : " + srv.getDeviceInfo().getImsi());
        System.out.println(
"    S/W Version   : " + srv.getDeviceInfo().getSwVersion());
        System.out.println(
"    Battery Level : " + srv.getDeviceInfo().getBatteryLevel() + "%");
        System.out.println(
"    Signal Level  : " + srv.getDeviceInfo().getSignalLevel() + "%");
        System.out.println(
"    GPRS Status   : " + (srv.getDeviceInfo().getGprsStatus() ? "Enabled" : "Disabled"));

        LinkedList messageList
= new LinkedList();

        srv.readMessages(messageList, CIncomingMessage.MessageClass.All);

       
for (int j=0;j<messageList.size();j++)
            {
                CIncomingMessage msg
= (CIncomingMessage)messageList.get(j);
                System.out.println(msg);
            }

       srv.disconnect();
      }
  
catch(Exception e)
      {
       e.printStackTrace();
       System.out.println(
"----------------------------------------");
       System.out.println();
      }
      }
     
    }
}

0 0

相关博文

我的热门文章

img
取 消
img