O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
BlackBerry Hacks
By Dave Mabe
October 2005
More Info

HACK
#96
Integrate the Browser into a Java App
Combine the richness of a Java client with the extensibility and power of a web backend
The Code
[Discuss (0) | Link to this hack]

The Code

	/**
	* Reverse Phone Directory Lookup Application
	* This sample uses a Java frontend to prompt the user for a phone
	* number, then launches a web page to display the corresponding address.	
	*/
	import net.rim.device.api.ui.*;
	import net.rim.device.api.ui.component.*;
	import net.rim.device.api.ui.container.*;
	import net.rim.device.api.i18n.*;
	import net.rim.device.api.system.*;

	import net.rim.device.api.collection.util.*;

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

		public BrowserLauncher( )
		{
			pushScreen(new SearchScreen( ));
		}

		class SearchScreen extends MainScreen
		{
			BasicEditField _phoneNumberEntry;

			SearchScreen( )
			{
			
				super(DEFAULT_MENU | DEFAULT_CLOSE);
				setTitle(new LabelField("Reverse phone lookup",
				LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH));

				_phoneNumberEntry = new BasicEditField("Phone number: ",
				  "", 10,
				  BasicEditField.NO_NEWLINE | BasicEditField.FILTER_INTEGER);
				add(_phoneNumberEntry);
				addMenuItem(new MenuItem("Search", 1, 1) {
				public void run( ) {
				String phone = _phoneNumberEntry.getText( );
				launchBrowser("http://www.infospace.com/" +
				"_1_L5UTTJ03LEBVO_ _infow.sbcw" +
				"/x/app/rev/detail.xhtml?top=&area=" +
				phone.substring(0,3) + "&exchange=" +
				phone.substring(3,6) +
				"&phend=" + phone.substring(6,10) +"&ran=24");
				}
				});
			}
		}
		static private boolean launchBrowser(String baseUrl)
		{
			boolean retval = true;

			int handle =
			  CodeModuleManager.getModuleHandle("net_rim_bb_browser_daemon");

			if (handle <=0 ) {
				retval = false;
			}
			else {
				ApplicationDescriptor[] browserDescriptors =
				CodeModuleManager.getApplicationDescriptors(handle);

				if (browserDescriptors == null )
				{
				retval = false;
				}
				else {
				if ( browserDescriptors.length <=0 ) {
				retval = false;
				}
				else {
				String[] args = {"url", baseUrl};
				ApplicationDescriptor descriptor =
				new ApplicationDescriptor(browserDescriptors[0],
				"url invocation", args,
				null, -1, null, -1,
				ApplicationDescriptor.FLAG_SYSTEM);
				try {
				ApplicationManager.getApplicationManager( ).
					runApplication(descriptor);
				}
				catch(ApplicationManagerException e) {
				retval = false;
				}
				}
				}
			}
			return retval;
		}	
	}

Save the code as BrowserLauncher.jav'a: this is the sole class in this application. The key function is launchBrowser( ), which includes some black magic to launch the user's default browser with the requested URL. The code links to the Infospace reverse directory page, providing the user's input as parameters.


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.