1

我需要帮助。

我创建了一个简单的应用程序来监听传入的短信,当短信到达时带有“OFF”这个词,那么设备应该关闭。

该应用程序打开但在收到一条带有“OFF”字样的短信后无法关闭设备

我不知道我做错了什么。这是我的代码:

new Thread() { //A new thread is created and surrounds the code 

    public void run() {

        try {

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

           for(;;) { //'For-Loop' used to listen continously for incoming sms's

                Datagram d = _dc.newDatagram(_dc.getMaximumLength());                     
                _dc.receive(d);  //The sms is received                           
                byte[] bytes = d.getData();
                String msg = new String(bytes); //The body of the sms is put on a string.

                if (msg == ("OFF") {

                    Device.requestPowerOff(false); //The device is shut down

                }                              
           }  

        } catch (NullPointerException me) { //NullPointerException caught       
              me.printStackTrace();
        } catch (IOException io) {
              io.printStackTrace(); 
        }        
    }
};

当我在没有线程的情况下运行应用程序时,它会冻结手机。

请帮助,我已经尝试了我所知道的一切,但似乎无法做到正确。

4

0 回答 0