2009-12-28 41 views

Trả lời

13

Để gửi một sms:

import net.rim.device.api.io.*; 
import net.rim.device.api.system.*; 
import javax.microedition.io.*; 
import java.util.*; 
import java.io.*; 
public class SendSms extends Application 
{ 
    private static final int MAX_PHONE_NUMBER_LENGTH = 32; 
    private String addr = "15191112222"; 
    private String msg = "This is a test message."; 
    private DatagramConnection _dc = null; 
    private static String _openString = "sms://"; 
    public static void main(String[] args) 
    { 
      new SendSms().enterEventDispatcher(); 
    } 
    public SendSms() 
    { 
      try { 
       _dc = (DatagramConnection)Connector.open(_openString); 
       byte[] data = msg.getBytes(); 
       Datagram d = _dc.newDatagram(_dc.getMaximumLength()); 
       d.setAddress("//" + addr); 
       _dc.send(d); 
      } catch (IOException e) {} 
      System.exit(0); 
    } 
} 

Để nhận được một sms:

import net.rim.device.api.io.*; 
import net.rim.device.api.system.*; 
import javax.microedition.io.*; 
import java.util.*; 
import java.io.*; 
public class ReceiveSms extends Application { 
    private ListeningThread _listener; 
    public static void main(String[] args) 
    { 
      new ReceiveSms().enterEventDispatcher(); 
    } 
    ReceiveSms() { 
      _listener = new ListeningThread(); 
      _listener.start(); 
    } 
    private class ListeningThread extends Thread 
    { 
      private boolean _stop = false; 
      private DatagramConnection _dc; 
      public synchronized void stop() 
      { 
       _stop = true; 
       try { 
        _dc.close(); 
       } catch (IOException e) { 
        System.err.println(e.toString()); 
       } 
      } 
      public void run() 
      { 
       try { 
        _dc = (DatagramConnection)Connector.open("sms://"); 
        for(;;) 
        { 
         if (_stop) { 
           return; 
         } 
         Datagram d = _dc.newDatagram(_dc.getMaximumLength()); 
         _dc.receive(d); 
         String address = new String(d.getData()); 
         String msg = new String(d.getData()); 
         System.out.println("Message received: " + msg); 
         System.out.println("From: " + address); 
         System.exit(0); 
        } 
       } catch (IOException e) { 
        System.err.println(e.toString()); 
       } 
      } 
    } 
} 
+0

Hi Ashraf th anks cho ur giúp u có thể cung cấp mã để cập nhật các msg nhận được trong màn hình bởi vì tôi đã cố gắng bản thân mình nhưng tôi không thể đạt được. – Kumar

+0

Bạn có ý gì khi "cập nhật tin nhắn nhận được"? Bạn có thể giải thích điều này chi tiết hơn không? –

+0

Hey Ashraf tôi muốn hiển thị các tin nhắn nhận được trong màn hình. Làm thế nào để làm điều này? – Kumar

3

Có vẻ như gửi tin nhắn SMS từ một chiếc BlackBerry không phải lúc nào cũng đơn giản. Tôi nghĩ rằng tôi đã tìm ra nó, nhưng nó không hoạt động khi người dùng BlackBerry đang sử dụng CDMA network as opposed to GSM mà tôi đang sử dụng.

Tôi đã tìm thấy solution here và điều chỉnh nó theo cách này. Khác với ví dụ đó cũng là số cổng, tôi sử dụng cổng 5016 as suggested on the Blackberry support forumBlackberry knowledge center.

private static byte[] stringToByte(String s) 
{ 
    char[] sa = s.toCharArray(); 
    byte[] ba = new byte[sa.length]; 

    for (int i = 0; i < ba.length; i++) { 
     ba[i] = (byte) (sa[i] & 0xFF); 
    } 

    return ba; 
} 

private static void sendCDMAText(String nr, String message) throws IOException 
{ 
    DatagramConnection conn = (DatagramConnection) Connector.open("sms://+" + nr + ":5016"); 
    byte[] bytes = stringToByte(message); 
    Datagram msg = conn.newDatagram(bytes, bytes.length); 
    conn.send(msg); 

} 

private static void sendSMS(String nr, String message) throws IOException 
{ 
    MessageConnection conn = (MessageConnection) Connector.open("sms://" + nr); 
    TextMessage msg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE); 
    msg.setPayloadText(message); 
    conn.send(msg); 
} 

public static void sendTextMessage(String nr, String message) throws IllegalArgumentException, InterruptedIOException, NullPointerException, SecurityException, IOException 
{ 
    if (RadioInfo.getNetworkType() == RadioInfo.NETWORK_CDMA) { 
     sendCDMAText(nr, message); 
     return; 
    } 

    sendSMS(nr, message); 
} 

Để gửi một tin nhắn văn bản, bạn sẽ gọi nó là như thế này:

sendTextMessage("555123123", "The little text message you wanted to send."); 

(. Trong trường hợp 555.123.123 là một số điện thoại Hollywood)

1
public SendSms() 
{ 
     try { 
      _dc = (DatagramConnection)Connector.open(_openString); 
      byte[] data = msg.getBytes(); 
      Datagram d = _dc.newDatagram(_dc.getMaximumLength()); 
      d.setAddress("//" + addr); 
      _dc.send(d); 
     } catch (IOException e) {} 
     System.exit(0); 
} 

Bạn không đặt thông báo ở đây ^^ (dữ liệu!)

Các vấn đề liên quan