07-06-2009 08:16 AM - edited 07-06-2009 08:17 AM
Hi,
I have written a simple class, which is running in Background without any probs but not intercepting any SMS, neither sending nor receiving, also it is not allowing my msg to go, all remain pending... I dn knw why..? The code is:
import net.rim.device.api.system.*;
import java.io.PrintStream;
import java.io.IOException;
import javax.microedition.io.*;
import javax.wireless.messaging.*;
import javax.wireless.messaging.MessageListener;
public class RunInBgMain extends Application implements MessageListener
{
private MessageConnection messconn;
private Message msg = null;
private String add = null;
String s_msg = null;
boolean done;
Reader reader;
private RunInBgMain()
{
//=====================================
try {
messconn = (MessageConnection)Connector.open("sms://:0");
System.out.println("messconn");
messconn.setMessageListener(this);
done = false;
System.out.println("done = false");
reader = new Reader();
System.out.println("Obj reader created.");
new Thread(reader).start();
}
catch(IOException e)
{
System.out.println("Err in IOException.");
}
}
public void notifyIncomingMessage(MessageConnection conn)
{
System.out.println("Inside notifyIncomingMessage");
// add = messconn.getAddress();
if (conn == messconn) {
System.out.println("conn==messconn");
reader.handleMessage();
}
}//Constructor closed
static void main(String[] args)
{
System.out.println("Inside main");
RunInBgMain runMain = new RunInBgMain();
runMain.enterEventDispatcher();
System.out.println("Hi, I am running in BgMain.");
}//main()closed
public class Reader implements Runnable
{
private int pendingMessages = 0;
//System.out.println("Inside Reader,Pending msg = " + pendingMessages);
public void run()
{
System.out.println("Inside run.");
while (!done)
{
System.out.println("Inside while.");
synchronized(this)
{
System.out.println("synchronized called.");
if (pendingMessages == 0)
{
System.out.println("Inside if(Pending msgs)");
try
{
System.out.println("try wait()");
wait();
}catch (Exception e){ // Handle interruption
}
}
pendingMessages--;
}
try
{
Message mess = messconn.receive();
System.out.println(" try for mess = messconn.receive() ");
}catch (IOException ioe) { // Handle reading errors
}
}
}//run() cosed.
public synchronized void handleMessage()
{
System.out.println("Inside synchronized().");
pendingMessages++;
notify();
}
// System.out.println("Reader class closed.");
}//class reader closed.
// System.out.println("BgApplication closed.");
}
Plz. guide..
Thanks in advance for ur unvaluable guidance..
07-06-2009 08:21 AM
07-06-2009 08:28 AM
Hi Mr. simon
I have altered it as,
messconn = (MessageConnection)Connector.open("sms://");
but still the status of each msg I want to send is shown Pending..
07-06-2009 11:12 AM - edited 07-06-2009 11:12 AM
kanaksony wrote:Hi Mr. simon
I have altered it as,
messconn = (MessageConnection)Connector.open("sms://");
Hi there, I tried to look into this problem. The first question - How did you manage to pass the following line ?
messconn.setMessageListener(this);
I mean, it throws IOException: operation not permitted on a client connection when the app starts.
If I set port (0 or something like sms://:0) I don't see this exception.
07-06-2009 11:58 AM - edited 07-06-2009 12:06 PM
import net.rim.device.api.io.*;
import net.rim.device.api.system.*;
import javax.microedition.io.*;
import java.util.*;
import java.io.*;
public class RunInBgMain extends Application {
private ListeningThread _listener;
// Additional code required for complete sample.
public static void main(String[] args) {
new RunInBgMain().enterEventDispatcher();
}
RunInBgMain() {
_listener = new ListeningThread();
_listener.start();
}
private static class ListeningThread extends Thread {
private boolean _stop = false;
private DatagramConnection _dc;
public synchronized void stop() {
_stop = true;
try {
_dc.close(); // Close the connection so the thread returns.
} catch (IOException e) {
System.err.println("stop().IOException: "+e.toString());
}
}
public void run() {
try {
_dc = (DatagramConnection)Connector.open("sms://");
for(;;) {
if ( _stop ) {
return;
}
Datagram d = _dc.newDatagram(_dc.getMaximumLength());
_dc.receive(d);
String address = new String(d.getAddress());
String msg = new String(d.getData());
System.out.println("Message received: " + msg);
System.out.println("From: " + address);
System.exit(0);
}
} catch (IOException e) {
System.err.println("run().IOException: "+e.toString());
}
}
}
}
One note 'though. I tried it by running two simulators and on the "receiver" I had to setup SMS source/destination port(s) as 21317/21316.
It didn't work without it.
P.S. I changed main class name to the RunInBgMain as in the first post.