Thursday, September 29, 2011

Invoke Blackberry Browser



Here is the tutorial explaining, how to invoke blackberry default browser. You can pass your url and can invoke the browser in your button click.




CODE



Code for Home screen



public class home extends UiApplication implements FieldChangeListener {
public long mycolor;
MainScreen Screen = new MainScreen();
public Background txtcolor = BackgroundFactory.createLinearGradientBackground(Color.GRAY, Color.GRAY, Color.GRAY, Color.GRAY);
Font myFont1;
Font myFont2;
Font font = Font.getDefault().derive(Font.ITALIC, 14);
Font myFont;

VerticalFieldManager mainManager;
VerticalFieldManager subManager;

private int deviceWidth = Display.getWidth();
private int deviceHeight = Display.getHeight();

final Bitmap backgroundBitmap = Bitmap.getBitmapResource("blackbg.png");

EditField urlfield = new EditField(Field.FOCUSABLE);
ButtonField submitbutton = new ButtonField("Browse",ButtonField.CONSUME_CLICK | ButtonField.HCENTER);

public static String uid;
public static final int DEFAULT_BROWSER = 0;
public static final int WAP_BROWSER = 1;
public static final int BES_BROWSER = 2;
public static final int WIFI_BROWSER = 3;
public static final int BIS_BROWSER = 4;
public static final int UNITE_BROWSER = 5;

public static void main(String arg[])
{
home application = new home();
application.enterEventDispatcher();

}

public home() {

super();

pushScreen(Screen);
//this is the main manager
mainManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR )
{
public void paint(Graphics graphics)
{
graphics.clear();
graphics.drawBitmap(0, 0, deviceWidth, deviceHeight, backgroundBitmap, 0, 0);
super.paint(graphics);
}
};
//this manger is used for adding the componentes
subManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR )
{
protected void sublayout( int maxWidth, int maxHeight )
{
int displayWidth = deviceWidth;
int displayHeight = deviceHeight;

super.sublayout( displayWidth, displayHeight);
setExtent( displayWidth, displayHeight);
}
};

//adding the sub manager to the main manager
mainManager.add(subManager);
// adding the main manager to the screen
Screen.add(mainManager);

FontFamily ff2 = null;
try {
ff2 = FontFamily.forName("Times New Roman");
} catch (ClassNotFoundException e) {

e.printStackTrace();
}

myFont2 = ff2.getFont(Font.EXTRA_BOLD, 180,(int) RichTextField.USE_ALL_WIDTH|(int)RichTextField.READONLY);

LabelField heading = new LabelField("Browser")
{
public void paint(Graphics graphics)

{
graphics.setColor(0x00FFFFFF);
super.paint(graphics);

}
};
heading.setFont(myFont2);
urlfield.setBackground(txtcolor);
urlfield.setLabel("URL: ");
submitbutton.setChangeListener(this);

subManager.add(heading);
subManager.add(urlfield);
subManager.add(submitbutton);

}

//##############################constructor ends here..###############################################
public boolean onSavePrompt()
{
return true;
}

public static void browse_fun(String val)
{
BrowserSession browserSession = createBrowserSession(BIS_BROWSER);
browserSession.displayPage(val);
browserSession.showBrowser();
}

private static BrowserSession createBrowserSession(int browserType)
{
uid = null;
BrowserSession browserSession = null;

switch (browserType)
{
case BIS_BROWSER:
{
browserSession = BrowserSessionFactory.createBISBrowserSession();
break;
}
case WAP_BROWSER:
{
browserSession = BrowserSessionFactory.createWAPBrowserSession();
break;
}
case BES_BROWSER:
{
browserSession = BrowserSessionFactory.createBESBrowserSession();
break;
}
case WIFI_BROWSER:
{
browserSession = BrowserSessionFactory.createWiFiBrowserSession();
break;
}
case UNITE_BROWSER:
{
browserSession = BrowserSessionFactory.createUniteBrowserSession();
break;
}
}

return null == browserSession ?
BrowserSessionFactory.createDefaultBrowserSession() : browserSession;
}


public void fieldChanged(Field field, int context) {

if(field == submitbutton)
{
if(urlfield.getText().trim().length() == 0)
{
Dialog.alert("Please enter a URL");
}
else
{
browse_fun(urlfield.getText());
}
}

}

}



Browser session factory class



public class BrowserSessionFactory
{
private static ServiceBook sb = ServiceBook.getSB();
private static ServiceRecord[] records =sb.findRecordsByCid("BrowserConfig");

public static BrowserSession createBISBrowserSession()
{
// If there are browser services found - search for the
// Service Record for the given browserType
String uid = null;
int numRecords = records.length;
for( int i = 0; i < numRecords; i++ )
{
ServiceRecord myRecord = records[i];
ServiceRecordHelper myRecordHelper =new ServiceRecordHelper(myRecord);

if( myRecord.isValid() && !myRecord.isDisabled() && myRecordHelper.getConfigType() ==ServiceRecordHelper.SERVICE_RECORD_CONFIG_TYPE_BIS )
{
uid = myRecord.getUid();
break;
}
}
return null == uid ? null : Browser.getSession(uid);
}

public static BrowserSession createWAPBrowserSession()
{
// If there are browser services found - search for the WAP2
// Service Record
String uid = null;
int numRecords = records.length;
for( int i = 0; i < numRecords; i++ )
{
ServiceRecord myRecord = records[i];
ServiceRecordHelper myRecordHelper =new ServiceRecordHelper(myRecord);
if( myRecord.isValid() && !myRecord.isDisabled() && (myRecordHelper.getConfigType() == ServiceRecordHelper.SERVICE_RECORD_CONFIG_TYPE_WAP2) && (myRecordHelper.getNavigationType() != -1))
{
uid = myRecord.getUid();
break;
}
}
// If there was no WAP2 Service Record found � search for the
// WAP Record
if (uid == null)
{
for( int i = 0; i < numRecords; i++ )
{
ServiceRecord myRecord = records[i];
ServiceRecordHelper myRecordHelper = new ServiceRecordHelper(myRecord);
if( myRecord.isValid() && !myRecord.isDisabled() && (myRecordHelper.getConfigType() ==ServiceRecordHelper.SERVICE_RECORD_CONFIG_TYPE_WAP) && (myRecordHelper.getNavigationType() != -1))
{
uid = myRecord.getUid();
break;
}
}
}
return null == uid ? null : Browser.getSession(uid);
}

public static BrowserSession createBESBrowserSession()
{
// If there are browser services found - search for the BES
// Service Record
String uid = null;
int numRecords = records.length;
for( int i = 0; i < numRecords; i++ )
{
ServiceRecord myRecord = records[i];
ServiceRecordHelper myRecordHelper =new ServiceRecordHelper(myRecord);
if( myRecord.isValid() && !myRecord.isDisabled() && myRecordHelper.getConfigType() ==ServiceRecordHelper.SERVICE_RECORD_CONFIG_TYPE_BES && !myRecord.getName().equals(ServiceRecordHelper.SERVICE_RECORD_NAME_UNITE))
{
uid = myRecord.getUid();
break;
}
}
return null == uid ? null : Browser.getSession(uid);
}


public static BrowserSession createWiFiBrowserSession()
{
String uid = null;

int numRecords = records.length;
for( int i = 0; i < numRecords; i++ )
{
ServiceRecord myRecord = records[i];
ServiceRecordHelper myRecordHelper = new ServiceRecordHelper(myRecord);

System.out.println("valid " + myRecord.isValid());
System.out.println("disabled " + myRecord.isDisabled());
System.out.println("config type " + myRecordHelper.getConfigType());

if( myRecord.isValid() && !myRecord.isDisabled() && myRecordHelper.getConfigType() == ServiceRecordHelper.SERVICE_RECORD_CONFIG_TYPE_WIFI )
{
System.out.println("uid " + myRecord.getUid());
uid = myRecord.getUid();
break;
}
}
//}
return null == uid ? null : Browser.getSession(uid);
}


public static BrowserSession createUniteBrowserSession()
{
String uid = null;
// If there are browser services found - search for the
// Unite Service Record
int numRecords = records.length;
for( int i = 0; i < numRecords; i++ )
{
ServiceRecord myRecord = records[i];
ServiceRecordHelper myRecordHelper = new ServiceRecordHelper(myRecord);
if( myRecord.isValid() && !myRecord.isDisabled() && myRecordHelper.getConfigType() == ServiceRecordHelper.SERVICE_RECORD_CONFIG_TYPE_BES && myRecord.getName().equals(ServiceRecordHelper.SERVICE_RECORD_NAME_UNITE))
{
uid = myRecord.getUid();
break;
}
}
return null == uid ? null : Browser.getSession(uid);
}

/*
* Default: This returns a session for the default browser
*
* @returns Default BrowserSession
*/
public static BrowserSession createDefaultBrowserSession()
{
return Browser.getDefaultSession();
}
}



Service record helper class



public class ServiceRecordHelper
{
private static final int SERVICE_RECORD_NAVIGATION_TYPE = 7;
private static final int SERVICE_RECORD_CONFIG_TYPE = 12;

public static final int SERVICE_RECORD_CONFIG_TYPE_WAP = 0;
public static final int SERVICE_RECORD_CONFIG_TYPE_BES = 1;
public static final int SERVICE_RECORD_CONFIG_TYPE_WIFI = 2;
public static final int SERVICE_RECORD_CONFIG_TYPE_BIS = 4;
public static final int SERVICE_RECORD_CONFIG_TYPE_WAP2 = 7;

public static final String SERVICE_RECORD_NAME_UNITE = "Unite";

private ServiceRecord record = null;

/**
* Constructs the ServiceRecordHelper with a ServiceRecord object
* @param record ServiceRecord
*/
public ServiceRecordHelper(ServiceRecord record)
{
this.record = record;
}

/**
* This method will return the config type of the service record.
* This information is contained in the service records application data (12th field)
*
*
* @return configType
*/
public int getConfigType()
{
return parseInt(getDataBuffer(SERVICE_RECORD_CONFIG_TYPE));
}


/**
* This method will return the navigation type of the service record.
* This information is contained in the service records application data (7th field).
* The navigation type indicates whether or not the user can navigate to other
* web pages from within the browser instance.
*
* @return navigationType
*/
public int getNavigationType()
{
return parseInt(getDataBuffer(SERVICE_RECORD_NAVIGATION_TYPE));
}

/**
* Converts the data buffer for the given type to an int value
* to the given type
*
* @param DataBuffer
* @return data buffer as an int or -1 if the data buffer is null or can't be read
*/
private int parseInt(DataBuffer buffer)
{
if (buffer != null)
{
try
{
return ConverterUtilities.readInt(buffer);
}
catch (EOFException e)
{
return -1;
}
}

return -1;
}

/**
* Returns a data buffer from the ServiceRecord based on the given type
*
* @param type data type
* @return Corresponding data buffer
*/
private DataBuffer getDataBuffer(int type)
{
DataBuffer buffer = null;
byte[] data = record.getApplicationData();

if (data != null)
{
buffer = new DataBuffer(data, 0, data.length, true);

try
{
buffer.readByte();
}
catch (EOFException e1)
{
buffer = null;
}

if (!ConverterUtilities.findType(buffer, type))
{
buffer = null;
}
}

return buffer;
}
}



Your output will be like this.













That's it. Your browser is invoked. You can download the sample project below.


Arun Kumar Munusamy Web Developer

Morbi aliquam fringilla nisl. Pellentesque eleifend condimentum tellus, vel vulputate tortor malesuada sit amet. Aliquam vel vestibulum metus. Aenean ut mi aucto.

2 comments:

  1. it only works on simulator.. why not on real device?!!!

    ReplyDelete