02-24-2010, 01:12 AM
|
#1 (permalink)
|
| Knows Where the Search Button Is
Join Date: Feb 2010 Model: 7100t PIN: N/A Carrier: VODAFONE
Posts: 18
Post Thanks: 0 Thanked 0 Times in 0 Posts
| sms retrieval problems::::The prob. i've been getting is there is no error generated Please Login to Remove! public final class GUILessApp extends Application implements javax.wireless.messaging.MessageListener
{
private BackGroundApp backGroundApp;
public static void main(String[] args)
{
GUILessApp theApp = new GUILessApp();
theApp.enterEventDispatcher();
}
public GUILessApp()
{
//Creates and starts a new BackGroundApp thread.
backGroundApp = new BackGroundApp();
backGroundApp.start();
}
//The thread that will run in the background.
private class BackGroundApp extends Thread
{
boolean stopThread = false;
public synchronized void stop()
{
stopThread = true;
}
public void run()
{
while (!stopThread)
{
System.out.println("Application is running");
try
{
sleep(5000);
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);
}
}
catch (Exception e) {
}
try
{
MessageConnection _mc = (MessageConnection)Connector.open("sms://:0");
for(;;)
{
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);
}
}
catch (Exception e)
{
//Exception handling would go here.
}
}
}
}
//Stop the thread on exit.
protected void onExit()
{
backGroundApp.stop();
}
MessageConnection _mc1;
Message m;
public void notifyIncomingMessage(MessageConnection conn)
{
//final MessageConnection _mc1;
try {
_mc1 = (MessageConnection)Connector.open("sms://:0");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
_mc1.setMessageListener(this);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
UiApplication.getApplication().invokeLater(new Runnable()
{
public void run()
{
try {
m = _mc1.receive();
} catch (InterruptedIOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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
try {
msg = new String(data, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
System.out.println("Invalid Message Format");
System.out.println("Received SMS text from " + address + " : " + msg);
}
});
}
} |
| Offline
| |