Saturday, March 3, 2012

Blackberry Contact Add/Edit and Deleter Listener

Today i have planning to study blackberry PIM listener for contact. This functionality most of use to get contact event when contact add/edit or delete on Blackberry Contact Application.
import java.util.Enumeration;
import javax.microedition.pim.Contact;
import javax.microedition.pim.ContactList;
import javax.microedition.pim.PIMException;
import javax.microedition.pim.PIMItem;
import javax.microedition.pim.PIMList;
import net.rim.blackberry.api.pdap.PIMListListener2;

final class MyPIMListener implements PIMListListener2 {

 public void itemAdded(PIMItem item) {
  System.out.println("Contact Add");
  if (item == null) {
   return;
  }else{
   //Read newly added PIMItem
   
  }
  
 }

 public void itemRemoved(PIMItem item) {
  System.out.println("Contact Remove");
  if (item == null) {
   return;
  }else{
   //Read removed Item
  }
 }

 public void itemUpdated(PIMItem oldItem, PIMItem newItem) {
  System.out.println("Contact UPdate");
  if (oldItem == null || newItem == null) {
   return;
  }
  itemRemoved(oldItem);
  itemAdded(newItem);
 }

 public void batchOperation(PIMList list) {
  if (list == null) {
   return;
  }

  try {
   ContactList contactList = (ContactList) list;
   Enumeration e = contactList.items();
   while (e.hasMoreElements()) {
    Contact contact = (Contact) e.nextElement();
    // ...
   }

  } catch (PIMException e) {
   System.out.println(e);
  }

 }

}


Write following code to call Listener.
try{
   MyPIMListener listener = new MyPIMListener();
   ContactList contactList;
   contactList = (ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY);
   BlackBerryPIMList blackberryContactList = (BlackBerryPIMList) contactList;
         blackberryContactList.addListener(listener);
  }catch(PIMException e){
   System.out.println("ex. "+e);
  }

Friday, March 2, 2012

Barcode scan sample example

Some time your application require to scan barcode and display that data on Blackberry Filed. That's why i am planning to write example for scan barcode in blackberry as following example may be save your time. This example run only Blackberry OS 6.0 and and 7.0 to scan barcode on Blackberry OS 5.0 please user this url may help full.

import java.util.Hashtable;
import java.util.Vector;

import net.rim.device.api.barcodelib.BarcodeDecoder;
import net.rim.device.api.barcodelib.BarcodeDecoderListener;
import net.rim.device.api.barcodelib.BarcodeScanner;
import net.rim.device.api.system.KeyListener;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.FullScreen;
import net.rim.device.api.ui.container.MainScreen;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.DecodeHintType;


public class BarcodeScanSample extends MainScreen{
 private FullScreen _barcodeScreen;
 private BarcodeScanner _scanner; 
 private LabelField lblBarcodeText;
 private ButtonField btnScan;
 
 public BarcodeScanSample(String barcodeText){
  lblBarcodeText = new LabelField(barcodeText);
  add(lblBarcodeText);
  btnScan = new ButtonField("Scan");
  btnScan.setChangeListener(new FieldChangeListener() {
   
   public void fieldChanged(Field field, int context) {
    scanBarcode();
   }
  });
  add(btnScan);
 }
 private void scanBarcode() {
  // If we haven't scanned before, we will set up our barcode scanner
  if (_barcodeScreen == null) {

   // First we create a hashtable to hold all of the hints that we can
   // give the API about how we want to scan a barcode to improve speed
   // and accuracy.
   Hashtable hints = new Hashtable();

   // The first thing going in is a list of formats. We could look for
   // more than one at a time, but it's much slower. and set Barcode Format.
   Vector formats = new Vector();
   formats.addElement(BarcodeFormat.QR_CODE);
   formats.addElement(BarcodeFormat.CODE_128);
   formats.addElement(BarcodeFormat.CODE_39);
   formats.addElement(BarcodeFormat.DATAMATRIX);
   formats.addElement(BarcodeFormat.EAN_13);
   formats.addElement(BarcodeFormat.EAN_8);
   formats.addElement(BarcodeFormat.ITF);
   formats.addElement(BarcodeFormat.PDF417);
   formats.addElement(BarcodeFormat.UPC_A);
   formats.addElement(BarcodeFormat.UPC_E);
   
   hints.put(DecodeHintType.POSSIBLE_FORMATS, formats);

   // We will also use the "TRY_HARDER" flag to make sure we get an
   // accurate scan
   hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);

   // We create a new decoder using those hints
   BarcodeDecoder decoder = new BarcodeDecoder(hints);

   // Finally we can create the actual scanner with a decoder and a
   // listener that will handle the data stored in the barcode. We put
   // that in our view screen to handle the display.
   try {
    _scanner = new BarcodeScanner(decoder, new MyBarcodeDecoderListener());
    _barcodeScreen = new MyBarcodeScannerViewScreen(_scanner);

   } catch (Exception e) {
    System.out.println("Could not initialize barcode scanner: " + e);
    return;
   }
  }

  // If we get here, all the barcode scanning infrastructure should be set
  // up, so all we have to do is start the scan and display the viewfinder
  try {
   _scanner.startScan();
   UiApplication.getUiApplication().pushScreen(_barcodeScreen);
  } catch (Exception e) {
   System.out.println("Could not start scan: " + e);
  }

 }
 /***
  * MyBarcodeScannerViewScreen
  * 

* This view screen is simply an extension of MainScreen that will hold our * scanner's viewfinder, and handle cleanly stopping the scan if the user * decides they want to abort via the back button. * * @author PBernhardt * */ private class MyBarcodeScannerViewScreen extends MainScreen { public MyBarcodeScannerViewScreen(BarcodeScanner scanner) { super(); try { // Get the viewfinder and add it to the screen _scanner.getVideoControl().setDisplayFullScreen(true); Field viewFinder = _scanner.getViewfinder(); this.add(viewFinder); // Create and add our key listener to the screen this.addKeyListener(new MyKeyListener()); } catch (Exception e) { System.out.println("Error creating view screen: " + e); } } /*** * MyKeyListener *

* This KeyListener will stop the current scan cleanly when the back * button is pressed, and then pop the viewfinder off the stack. * * @author PBernhardt * */ private class MyKeyListener implements KeyListener { public boolean keyDown(int keycode, int time) { // First convert the keycode into an actual key event, taking // modifiers into account int key = Keypad.key(keycode); // From there we can compare against the escape key constant. If // we get it, we stop the scan and pop this screen off the stack if (key == Keypad.KEY_ESCAPE) { try { _scanner.stopScan(); } catch (Exception e) { System.out.println("Error stopping scan: " + e); } UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { UiApplication.getUiApplication().popScreen(_barcodeScreen); } }); return true; } // Otherwise, we'll return false so as not to consume the // keyDown event return false; } // We will only act on the keyDown event public boolean keyChar(char key, int status, int time) { return false; } public boolean keyRepeat(int keycode, int time) { return false; } public boolean keyStatus(int keycode, int time) { return false; } public boolean keyUp(int keycode, int time) { return false; } } } /*** * MyBarcodeDecoderListener *

* This BarcodeDecoverListener implementation tries to open any data encoded * in a barcode in the browser. * * @author PBernhardt * **/ private class MyBarcodeDecoderListener implements BarcodeDecoderListener { public void barcodeDecoded(final String rawText) { // First pop the viewfinder screen off of the stack so we can see // the main app UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { UiApplication.getUiApplication().popScreen(_barcodeScreen); } }); _barcodeScreen.invalidate(); //Display this barcode on LabelField on BarcodeScanSample MainScreen we can also set whatever field here. UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { UiApplication.getUiApplication().popScreen(); UiApplication.getUiApplication().pushScreen(new BarcodeScanSample(rawText)); _barcodeScreen.close(); _barcodeScreen=null; } }); } } }


You can test this code on device(terminal) and after scan completed barcode data will display on Label Field.

Enjoy..