Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
kanaksony
Posts: 571
Registered: ‎04-11-2009
My Device: BB 10 Dev Alpha

messageListener not intercepting

[ Edited ]

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..

Message Edited by kanaksony on 07-06-2009 05:47 PM
Regards,
Kanak Sony
------------------------------------------------------------------------------------------
http://dodevelopnshare.wordpress.com/ | http://www.linkedin.com/profile/view?id=188131481&trk=tab_pro
Please use plain text.
Developer
simon_hain
Posts: 16,101
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: messageListener not intercepting

open a message connection without a port to receive all incoming sms
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
kanaksony
Posts: 571
Registered: ‎04-11-2009
My Device: BB 10 Dev Alpha

Re: messageListener not intercepting

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..

 

Regards,
Kanak Sony
------------------------------------------------------------------------------------------
http://dodevelopnshare.wordpress.com/ | http://www.linkedin.com/profile/view?id=188131481&trk=tab_pro
Please use plain text.
Developer
konic
Posts: 173
Registered: ‎06-11-2009
My Device: Not Specified

Re: messageListener not intercepting

[ Edited ]

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.

 

Message Edited by konic on 07-06-2009 11:12 AM
Please use plain text.
Developer
konic
Posts: 173
Registered: ‎06-11-2009
My Device: Not Specified

Re: messageListener not intercepting

[ Edited ]
OK, here is the code which does work. It is from the "BlackBerry Java Development Environment. Version 4.0.2. BlackBerry Application Developer Guide. Volume 1: Fundamentals" (Receiving SMS message)

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.

Message Edited by konic on 07-06-2009 12:06 PM
Please use plain text.