听取传入的短信时,手机会冻结

时间:2012-07-10 10:17:00

标签: blackberry sms

为什么以下代码会冻结手机上的所有操作?该应用程序读取传入的短信,但应用程序无法打开。点击应用程序后,手机会冻结。我究竟做错了什么。我将不胜感激任何帮助。

try {  
   //A DatagramConnection is created to listen for any incoming sms's.
   DatagramConnection _dc =
      (DatagramConnection)Connector.open("sms://"); 

   Datagram d = _dc.newDatagram(_dc.getMaximumLength());  

   _dc.receive(d);                             
   byte[] bytes = d.getData();
   String address = d.getAddress(); //The address of the sms is put on a string.
   String msg = new String(bytes); //The body of the sms is put on a string. 
} catch (Exception me) {

}  

1 个答案:

答案 0 :(得分:3)

在线程内运行代码(这是我编写的最糟糕的代码):

new Thread() {
    public void run() {
        DatagramConnection _dc =
        (DatagramConnection)Connector.open("sms://"); //A DatagramConnection is created to listen for any incoming sms's.

        Datagram d = _dc.newDatagram(_dc.getMaximumLength());  

        _dc.receive(d);                             
        byte[] bytes = d.getData();
        String address = d.getAddress(); //The address of the sms is put on a string.
        String msg = new String(bytes); //The body of the sms is put on a string.
    }
}.start();