-2

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

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) {

}  
4

1 回答 1

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();
于 2012-07-10T10:51:05.907 回答