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
sheeluwiki
Posts: 254
Registered: ‎03-15-2011
My Device: simulator
My Carrier: simulator
Accepted Solution

FolderListener for PIN messages in OS 6

[ Edited ]

I have read some articles in the forum regarding the same issue but as they did not solve my problem, i am writing it in a new topic.

 

My application wants to listen to PIN messages. Here is a sample code , which sends PIN message to itself and listens to the same..

 

The MessageAdded function is called only the first time if I run this application several times though PIN message is sent and recieved every time and I see them in the Inbox.

 

Also, when the messageAdded is called (only the first time and it is never called again.. Dont know why) , the mS.setTitle("Inside Message added."+e.getMessage().getSubject()); is not executed.

 

Please help me understand my issue.

 

I could not trace out the problem as I dont understand how to debug on real device..

 

 

 

public class MyApp extends UiApplication implements FolderListener
{
	MyScreenP mS;
	double lat = 40.6399;
	double lon=58.2779;
	public Store store;
    public static void main(String[] args) throws Exception
    {
        MyApp theApp = new MyApp();       
        theApp.enterEventDispatcher();
    }
    
    public MyApp() throws Exception
    {        
   	
    	 store = Session.getDefaultInstance().getStore(); 
    	 store.addFolderListener(this);
       
    		UiApplication.getUiApplication().invokeLater(new Runnable() 
    		{
    		    public void run() {
    		       Dialog.alert("App started !!");
    		    }
    		});
        	pushScreen(mS = new MyScreenP());
    }
    private final class MyScreenP extends MainScreen
    {
    	public LabelField lf,latL,lonL,insidesttat ;
        public MyScreenP() throws MessagingException
        {        
        	VerticalFieldManager vfm = new VerticalFieldManager();
        	setTitle("My screen....");
            lf = new LabelField("Just started");
            latL= new LabelField("latL");
            lonL= new LabelField("lonL");
            insidesttat= new LabelField("insidesttat");
            vfm.add(lf);
            vfm.add(insidesttat);
            vfm.add(latL);
            vfm.add(lonL);
            add(vfm);
            
            int pin = DeviceInfo.getDeviceId();
    		String s = Integer.toHexString(pin);
    		Store store1 = Session.getDefaultInstance().getStore(); 
        	Folder[] folders = store1.list(Folder.SENT);
        	Folder sentfolder = folders[0];
        	Message msg = new Message(sentfolder);
        	PINAddress recipients[] = new PINAddress[1];
        	try{    
        		recipients[0]= new PINAddress(s,"TestPinMsg");
        	}catch (AddressException ae){     
        		System.err.println(ae);
        	}
        	
        	try{    
        		msg.addRecipients(Message.RecipientType.TO, recipients);     
        		msg.setSubject("Test pin message");     
        		msg.setContent("lon :"+lon+"\n lat :"+lat ); 
        		Transport.send(msg); 
        		}catch (MessagingException me){     
        			System.err.println(me);
        			throw me;
        		}
        }
    }

	public void messagesAdded(FolderEvent e) {
		mS.setTitle("Inside Message added"); 
		mS.setTitle("Inside Message added."+e.getMessage().getSubject()); 
		// Parsing of the message received to get the latitude and longitude
	      int lonPos=0,latPos = 0, enterPos=0;
	      // Do any operation only if the message body is our default value
	      //if(!orginalMessage.getFolder().equals(sentfolder) &&orginalMessage.getSubject().trim().equalsIgnoreCase("Sheela_TestPinMsg")){ 
	      if(orginalMessage.getSubject().trim().equalsIgnoreCase("Test pin message")){ 
	    	  mS.setTitle("Inside IF");
//	    	 String msgBody= orginalMessage.getContent().toString(); // Getting the body of the message
//	    	  for(int i=0;i<msgBody.length();i++){
//	  			if(msgBody.charAt(i) == ':'){
//	  				if(latPos == 0){
//	  					latPos=i;
//	  					// mS.add(new LabelField("LAT POS...."+latPos));
//	  				}else{
//	  					lonPos=i;
//	  					// mS.add(new LabelField("LON POS...."+lonPos));
//	  				}
//	  			}else if(msgBody.charAt(i) == '\n'){
//	  				enterPos=i;
//	  				 mS.add(new LabelField("ENTER POS...."+enterPos));
//	  			}
//	  		}
//	  		
//	  		String lat= msgBody.substring(latPos, enterPos); // In Substring function the end position is exclusive
//	  		String lon= msgBody.substring(lonPos, msgBody.length()-1);	
//	  		mS.lon.setText("Longtitidue.. "+lon);
//	  		mS.lat.setText("Longtitidue.. "+lat);
	      }else{
	    	  mS.setTitle("Unfortuntely inside else loop");
	      }
	}


	public void messagesRemoved(FolderEvent e) {
		// TODO Auto-generated method stub
		
	}    
}

 

------------------------------------------------------------------------------------------------
Click "Accept as Solution" if post was what you needed. Give kudos if you feel response is helpful.

Please use plain text.
Developer
sheeluwiki
Posts: 254
Registered: ‎03-15-2011
My Device: simulator
My Carrier: simulator

Re: FolderListener for PIN messages in OS 6

Could some one help  me understand why the messageAdded() function is called at random basis though the PIN message is sent everytime when I run the application?

------------------------------------------------------------------------------------------------
Click "Accept as Solution" if post was what you needed. Give kudos if you feel response is helpful.

Please use plain text.
Developer
sheeluwiki
Posts: 254
Registered: ‎03-15-2011
My Device: simulator
My Carrier: simulator

PIN message problem when sending and receiving devices are the same

I have figured out the exact problem. The problem is with sending and recieving PIN messages to the same device..

 

Firstly when the PIN message is sent, the messageAdded() event is triggered as it is added to OUTBOX  for sending.. (and not inbox) In this meanwhile, the message is recieved which occurs faster and the messageAdded is not triggered again when the new message is received in INBOX..

 

This is my problem.. So I want to listen only to PIN messages that is added only in Inbox.. But the usual store listener for email which i see in many forum does not suit for PIN messages..

 

Can anyone please help me with this issue??????

------------------------------------------------------------------------------------------------
Click "Accept as Solution" if post was what you needed. Give kudos if you feel response is helpful.

Please use plain text.
Contributor
marKram
Posts: 33
Registered: ‎02-13-2010
My Device: 9800
My Carrier: Rogers

Re: PIN message problem when sending and receiving devices are the same

Just check if the message is inbound. This will ignore the outbound ones added to the Inbox folder. For example:

 

if (e.getMessage().isInbound()) ...

Please use plain text.
Developer
sheeluwiki
Posts: 254
Registered: ‎03-15-2011
My Device: simulator
My Carrier: simulator

Re: PIN message problem when sending and receiving devices are the same

Thanks for the reply.. But the code does not work in my case..

 

Because the PIN message is sent and recieved in fraction of seconds (As in my case the sender and receiver is the same device), the messageAdded() function is still not called again when it is received..

 

Is it possible to add the listener only when the PIN message is received??

 

I tried the following code also.. But still facing the same problem..

 

 Store store = Session.getDefaultInstance().getStore();
    	 Folder[] folders = store.list();
    	 for( int foldercnt = folders.length - 1; foldercnt >= 0; --foldercnt) {
    	    Folder f = folders[foldercnt];
    	    recurse(f);
    	 }

---------

private void recurse(Folder f)
	 {
	    if ( f.getType() != Folder.OUTBOX && f.getType() != Folder.SENT )
	    {
	        f.addFolderListener(this);
	        
	    }
	    Folder[] farray = f.list();
	    for (int fcnt = farray.length - 1; fcnt >= 0; --fcnt)
	    {
	        recurse(farray[fcnt]);
	    }
	 }

 

------------------------------------------------------------------------------------------------
Click "Accept as Solution" if post was what you needed. Give kudos if you feel response is helpful.

Please use plain text.
Developer
sheeluwiki
Posts: 254
Registered: ‎03-15-2011
My Device: simulator
My Carrier: simulator

Re: PIN message problem when sending and receiving devices are the same

Could someone please help me to overcome this issue??

------------------------------------------------------------------------------------------------
Click "Accept as Solution" if post was what you needed. Give kudos if you feel response is helpful.

Please use plain text.
Developer
sheeluwiki
Posts: 254
Registered: ‎03-15-2011
My Device: simulator
My Carrier: simulator

Re: PIN message problem when sending and receiving devices are the same

That will be great if anyone could help me..

------------------------------------------------------------------------------------------------
Click "Accept as Solution" if post was what you needed. Give kudos if you feel response is helpful.

Please use plain text.
Developer
sheeluwiki
Posts: 254
Registered: ‎03-15-2011
My Device: simulator
My Carrier: simulator

Re: PIN message problem when sending and receiving devices are the same

How can I set listener only for the receiving PIN messages??

 

Please help..

------------------------------------------------------------------------------------------------
Click "Accept as Solution" if post was what you needed. Give kudos if you feel response is helpful.

Please use plain text.
Administrator
MSohm
Posts: 14,313
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: PIN message problem when sending and receiving devices are the same

You could add your listener to just the folder that received PIN messages are stored in. It'll look something like "no service book://Inbox".

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
Contributor
abaratz
Posts: 25
Registered: ‎11-09-2010
My Device: Not Specified

Re: PIN message problem when sending and receiving devices are the same

One thing you should be aware of, is that messagesAdded is called from a different context (not your "MyApp" context"), so you can;t access static class members unless u use runtime store. see http://supportforums.blackberry.com/t5/Java-Development/Static-reference-returning-different-values-.... Other apporuch u can take is post the message using application post global to your main event queue and than u can work on it on in your main class context.

AviB

 

Please use plain text.