Archive for the ‘Blackberry’ Category

Blackberry: Custom Popup Screen[Forgot Password Example]

Here I am providing one example that shows how we customize our Popup Screen. We can use this like a popup Screen or Dialog Screen.

Below link is the Example for the Custom Popup Screen and I am showing an example of Forgot Password.

https://www.dropbox.com/s/9sy9t7y87jdh0r0/CustomPopupExample.zip

In this you will get again two custom classes. Like, PictureBackgroundButtonField and CustomTextField where we can use images directly to the buttons and text fields.

It will look like the below image:

9700

 

Lets try this one.

Feel free to ask questions.

Blackberry: Post Data Using JSON Object

We can post the data by the JSON format.

For this first,

1. We have to take all the keys and values in One Hash table or Array of Hash tables(Vector)

2. Convert it into JSON object.

3. Send to server.

packagecom.alishaik.thread;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.util.Hashtable;
importjavax.microedition.io.Connector;
importjavax.microedition.io.HttpConnection;
importnet.rim.blackberry.api.browser.URLEncodedPostData;
importnet.rim.device.api.io.http.HttpProtocolConstants;
importnet.rim.device.api.ui.Screen;
importnet.rim.device.api.ui.UiApplication;
importcom.hiddenbrains.ddft.common.CommonMethods;
importcom.hiddenbrains.ddft.common.MessageScreen;
importcom.hiddenbrains.ddft.common.Utility;
importcom.hiddenbrains.ddft.json.JSONObject;
public class PostDataThread extendsThread
{
privateHttpConnection httpConnection;
privateInputStream inputStream;
privateStringBuffer stringBuffer=newStringBuffer();
privateHashtable hashTable;
privateString URL="";
privateintindex=0;

public PostDataThread(String url, Hashtable hash,intindex)
{
// System.out.println("=========WEB SERVICE===========: "+url);
URL=Utility.escapeHTML(url)+CommonMethods.updateConnectionSuffix();//interface=wifi; or deviceside=false;
this.index=index;
this.hashTable=hash;//Here I am taking one Hashtable. You can manage according to your requirement.
}

publicvoidrun()
{
try
{
httpConnection=(HttpConnection)Connector.open(URL);
httpConnection.setRequestMethod(HttpConnection.POST);

URLEncodedPostData oPostData =newURLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, false);
oPostData.setData(provideJSONObject());//IMPORTANT

byte[] postBytes = oPostData.getBytes();
httpConnection.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE,"application / requestJson");
httpConnection.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, Integer.toString(postBytes.length));

OutputStream strmOut = httpConnection.openOutputStream();
strmOut.write(postBytes);
strmOut.flush();
strmOut.close();

intresponse=httpConnection.getResponseCode();
if(response==HttpConnection.HTTP_OK)
{
inputStream = httpConnection.openInputStream();
intc;
while((c=inputStream.read())!=-1)
{
stringBuffer.append((char)c);
}
parseData(stringBuffer.toString());
}
else
parserInterface.didFailParser("Connection failure HTTP Response: "+response);
}
catch(finalException e)
{
String msg=e.getMessage();
if(msg==null)
msg="No Data Found";
//Show failure alert here
}
finally
{
try
{
if(httpConnection!=null)
httpConnection.close();
if(inputStream!=null)
inputStream.close();
}
catch(Exception e2)
{}
}
}

private String provideJSONObject()
{
try
{
JSONObject jsonObj=newJSONObject();
jsonObj.put("data", hashTable);//Getting correct data; You can check here
// System.out.println("======================JSON STRING FOR POSTING: "+jsonObj.toString());
returnjsonObj.toString();
}
catch(Exception e)
{
returnnull;
}
}

private void parseData(String responseString)
{
responseString="["+responseString+"]";
System.out.println("======================\n\n"+responseString+"\n\n===================");
//Do what you want.
}
}

public static String escapeHTML(String s)
{
StringBuffer sb = new StringBuffer();
int n = s.length();
for (int i = 0; i < n; i++) {
char c = s.charAt(i);
switch (c) {
case ‘ ‘: sb.append(“%20”); break;
default: sb.append(c); break;
}
}
return sb.toString();
}

You can get this class by this below link:

https://www.dropbox.com/s/2i5txbjip7rs33g/PostDataThread.java

Blackberry: Video Recording in Blackberry

For video recording first we have to check the memory available in Blackberry. According to that we have to go to the next step.

Download the zip file for the example for Video recording in Blackberry

https://www.dropbox.com/s/tru6eb4gxm5yz1p/VideoRecording.zip

Blackberry : Any Type of Barcode Generator

We can scan Any type of barcode using blackberry. You can download the code from here.

First see the BarcodeScanner in blackberry 6.0 sample codes.

Take the code from this link : https://www.dropbox.com/sh/toddwzgakvkbwgz/mkNvzCJRST 

Blackberry: QR-Code Reader for BB-5.0

Yes, we can read the QRCode in Blackberry 5.0 api applications. For this we have to follow some points from this link:

http://supportforums.blackberry.com/t5/Java-Development/barcode-code-scanning/m-p/1561399

You can download the code from this below link:

https://www.dropbox.com/s/t6mwhpriorlsz8n/QRCodeForVersion5.zip

Create CUSTOM MENU in Blackberry Example

This is a sample example of showing CUSTOM menu in every screen as we like:

=============StartUp.java=================

package com.alishaik.custommenu;

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;

public class StartUp extends UiApplication
{
public static void main(String[] args)
{
StartUp theApp = new StartUp();
theApp.enterEventDispatcher();
}

public StartUp()
{
pushScreen(new FirstScreen());
}

public static void showAlertMessage(final String message)
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.alert(message);
}
});
}
}

==================FirstScreen.java===========

package com.alishaik.custommenu;

public class FirstScreen extends MainScreen implements MenuInterface
{
private ButtonField clickButton;
private CustomMenu firstMenu1,firstMenu2;

public FirstScreen()
{
createGUI();
}

private void createGUI()
{
firstMenu1=new CustomMenu(“First1”, 100, 101, 1);
firstMenu1.setMenuListener(this);
addMenuItem(firstMenu1);

firstMenu2=new CustomMenu(“First2”, 100, 101, 2);
firstMenu2.setMenuListener(this);
addMenuItem(firstMenu2);

add(new LabelField(“First Screen”, Field.FOCUSABLE));
//ButtonField.CONSUME_CLICK if you are not writing this every screen menu shows.
clickButton=new ButtonField(“Clik to go Next”, Field.FOCUSABLE|ButtonField.CONSUME_CLICK);
clickButton.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
UiApplication.getUiApplication().pushScreen(new SecondScreen());
}
});
add(clickButton);
}

public void onMenuPressed(String menuText, int tag)
{
StartUp.showAlertMessage(“You Selected “+menuText);
if(tag==1)
{
//FirstMenu1 Selected;
}
else
{
//FirstMenu2 Selected;
}
}

protected boolean onSavePrompt()
{
return true;
}
}
==================SecondScreen.java===========

package com.alishaik.custommenu;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
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.MainScreen;

public final class SecondScreen extends MainScreen implements MenuInterface
{
private ButtonField clickButton;
private CustomMenu secondMenu1,secondMenu2;

public SecondScreen()
{
createGUI();
}

private void createGUI()
{
secondMenu1=new CustomMenu(“Second1”, 100, 101, 1);
secondMenu1.setMenuListener(this);
addMenuItem(secondMenu1);

secondMenu2=new CustomMenu(“Second2”, 100, 101, 2);
secondMenu2.setMenuListener(this);
addMenuItem(secondMenu2);

add(new LabelField(“Second Screen”, Field.FOCUSABLE));
//ButtonField.CONSUME_CLICK if you are not writing this every screen menu shows.
clickButton=new ButtonField(“Back button”, Field.FOCUSABLE|ButtonField.CONSUME_CLICK);
clickButton.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
UiApplication.getUiApplication().popScreen(SecondScreen.this);
return;
}
});
add(clickButton);
}

public void onMenuPressed(String menuText, int tag)
{
StartUp.showAlertMessage(“You Selected “+menuText);
if(tag==1)
{
//SecondMenu1 Selected;
}
else
{
//SecondMenu2 Selected;
}
}

protected boolean onSavePrompt()
{
return true;
}
}
==================MenuInterface.java=============

package com.alishaik.custommenu;

public interface MenuInterface
{
public void onMenuPressed(String menuText,int tag);
}
=================CustomMenu.java=====================

package com.alishaik.custommenu;

import net.rim.device.api.ui.MenuItem;

public class CustomMenu extends MenuItem
{
private MenuInterface menuInterface;
private String menuText;
private int tag;

public CustomMenu(String text, int ordinal, int priority, int tag)
{
super(text, ordinal, priority);
this.menuText=text;
this.tag=tag;
}

public void run()
{
menuInterface.onMenuPressed(menuText, tag);
}

public void setMenuListener(MenuInterface listener)
{
this.menuInterface=listener;
}
}
===================*******================

Get The Image From SDCard In Blackberry

For this we have to first check that “is SDCard available or not?”

For checking the SDCARD availability we have to use the following code:

Source code of SDCARD Checking: SDCardTest.java

import java.util.Enumeration;
import javax.microedition.io.file.FileSystemRegistry;
public class SDCardTest
{
 public static boolean isSDCardAvailable()
 {
 boolean sdCardPresent = false;
 String root = null;
 Enumeration e = FileSystemRegistry.listRoots();

 while (e.hasMoreElements())
 {
 root = (String)e.nextElement();
 if(root.equalsIgnoreCase("sdcard/"))
 {
 sdCardPresent = true;
 }
 }
 return sdCardPresent;
 }
}

and the code for Get the Image from SDCARD : LoadingScreen.java

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import net.rim.device.api.io.IOUtilities;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.picker.FilePicker;
import net.rim.device.api.ui.picker.FilePicker.Listener;

import com.hiddenbrains.image.common.SDCardTest;

public class LoadingScreen extends MainScreen implements FieldChangeListener
{
 private VerticalFieldManager ver;
 private ButtonField showImage;
 private BitmapField bitmapField;

 public LoadingScreen()
 {
        ver=new VerticalFieldManager(USE_ALL_WIDTH);
        showImage=new ButtonField("Show Image",Field.FIELD_HCENTER);
        showImage.setChangeListener(this);
        ver.add(showImage);

        bitmapField=new BitmapField(null,Field.FIELD_HCENTER);
        bitmapField.setPadding(10, 0, 10, 0);
       ver.add(bitmapField);

      add(ver);
 }
 public void fieldChanged(Field field, int context)
 {
 if(field==showImage)
 {
 selectImageFromSDCARD();
 }
 }

 private void selectImageFromSDCARD()
 {
 String PATH="";
 if(SDCardTest.isSDCardAvailable())
 PATH = System.getProperty("fileconn.dir.memorycard.photos");//The default stored Images Path;
 else
 PATH = System.getProperty("fileconn.dir.photos");//The default stored Images Path;

 FilePicker filePicker=FilePicker.getInstance();
 filePicker.setPath(PATH);
 filePicker.setListener(new Listener()
 {
 public void selectionDone(String url)
 {
 System.out.println("======================URL: "+url);
 try
 {
 FileConnection file = (FileConnection)Connector.open(url);
 if(file.exists())
 {
 InputStream inputStream = file.openInputStream();
 byte[] data=new byte[inputStream.available()];
 data=IOUtilities.streamToBytes(inputStream);
 Bitmap bitmap=Bitmap.createBitmapFromBytes(data, 0, data.length,1);//Here we get the Image;

 Bitmap scaleBitmap=new Bitmap(400, 300);//Now we are scaling that image;
 bitmap.scaleInto(scaleBitmap, Bitmap.FILTER_LANCZOS);
 bitmapField.setBitmap(scaleBitmap);
 }
 else
 {
 bitmapField.setBitmap(Bitmap.getBitmapResource("icon.png"));
 }
 }
 catch (IOException e)
 {
 bitmapField.setBitmap(Bitmap.getBitmapResource("icon.png"));
 }
 }
 });
 filePicker.show();
 }

 protected boolean onSavePrompt() //It doesn't show the "Save","Discard","Cancel" POPUP;
 {
 return true;
 }

 public boolean onMenu(int instance) //It doesn't show the Menu;
 {
 return true;
 }
}