2012-07-10 39 views
-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) { 

} 
+1

嘗試運行內螺紋 – 2012-07-10 10:21:11

+0

代碼能否請您爲我提供關於如何做一個代碼示例? – BirthOfTragedy 2012-07-10 10:35:38

回答

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();