Sample code for listening to and retrieving PIN messages on a BlackBerry device

by Developer on ‎10-15-2012 10:18 AM - edited on ‎10-15-2012 10:18 AM by BlackBerry Development Advisor (Retired) (4,682 Views)
//This is just sample code for use as a reference and is in no way optimized and not the only way to achieve this
// -You could also attach the FolderListener to the entire store and filter messages by type (email vs PIN)

import net.rim.blackberry.api.mail.Message;
import net.rim.blackberry.api.mail.Folder;
import net.rim.blackberry.api.mail.Store.


public class PINMessaging {
   Store PINStore;

   public PINMessaging(){
      Session session = Session.getDefaultInstance();
      PINStore = session.getStore();
   }

   //Attach FolderListener to all PIN Message folders 
   public void attachListener( FolderListener listener ){
      for( int i=0; i<PINStore.list().length(); ++i ){
         if( (PINStore.list()[i].getFullName().toLowerCase().indexOf("no service book") != -1) ){
            PINStore.list()[i].addFolderListener( listener );
         }   
      }
   }
   
   //Attach FolderListener to a specific folder type [ie Inbox, Outbox, etc]
   public void attachListener( FolderListener listener, int folderType ){
      for( int i=0; i<PINStore.list(folderType).length(); ++i ){
         if( (PINStore.list(folderType)[i].getFullName().toLowerCase().indexOf("no service book") != -1) ){
            PINStore.list(folderType)[i].addFolderListener( listener );
         }   
      }
   }

//Retrieve all messages under a specific folder public Message[] retrieveMessages(int folderType){ for( int i=0; i<PINStore.list(folderType).length(); ++i ){ if( (PINStore.list(folderType)[i].getFullName().toLowerCase().indexOf("no service book") != -1) ){ return PINStore.list(folderType)[i].getMessages(); } } }
}