0

我用下面的代码通知短信。

它在两个黑莓模拟器上工作。

我已经在我的设备上安装了该应用程序并从 android 设备发送短信。

短信侦听器在设备上不起作用。

在设备上收到传入消息。但我的应用程序没有通知听众。

有什么问题怎么解决。

需要为设备提供什么端口号?

class BackgroundApplication extends Application  implements MessageListener
{  
    int i=0;
    static String suffix;
    MessageConnection _mc ;

    public BackgroundApplication() 
    {
        try {
            _mc = (MessageConnection)Connector.open("sms://:0");
            _mc.setMessageListener(this);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 


    }

public void notifyIncomingMessage(MessageConnection conn) {
        try {

            Message m = _mc.receive();
            String address = m.getAddress();
            String msg = null;
            if ( m instanceof TextMessage )
            {
                TextMessage tm = (TextMessage)m;
                msg = tm.getPayloadText();
            }
            else if (m instanceof BinaryMessage) {
                StringBuffer buf = new StringBuffer();
                byte[] data = ((BinaryMessage) m).getPayloadData();

                // convert Binary Data to Text
                msg = new String(data, "UTF-8");
            }
            else
                System.out.println("Invalid Message Format");
            System.out.println("Received SMS text from " + address + " : " + msg);
            showDialog("Msg: "+msg);

        } catch (Exception e) {
            // TODO: handle exception
        }
    }


    private void showDialog(String string) {


        synchronized (UiApplication.getEventLock()) 
        {
            Status.show(""+string,Bitmap.getPredefinedBitmap(Bitmap.INFORMATION), 5000,
                    Status.GLOBAL_STATUS, true, false, 1);
        }

    }

}
4

1 回答 1

0

检查这个

http://supportforums.blackberry.com/t5/Java-Development/Different-ways-to-listen-for-SMS-messages/ta-p/445062

DatagramConnection _dc =
     (DatagramConnection)Connector.open("sms://");
            for(;;)
        {
               Datagram d = _dc.newDatagram(_dc.getMaximumLength());
               _dc.receive(d);
               byte[] bytes = d.getData();
               String address = d.getAddress();
               String msg = new String(bytes);
               System.out.println( "Received SMS text from " + address + " : " + msg);
         }
于 2013-06-26T06:18:06.133 回答