Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Launch the BlackBerry Browser with GPS information

by Retired ‎02-12-2010 04:06 PM - edited ‎02-18-2010 09:10 AM (2,186 Views)

Summary


This article applies to the following:

  • BlackBerry® Browser
  • BlackBerry smartphones with GPS capability


Details


The code samples below are from an application that launches the BlackBerry Browser and retrieves GPS location information to display the current weather. The Browser itself does not contain the GPS information. A URL is created with the GPS information from the GPS on the BlackBerry smartphone and the server to determines the exact location using the given coordinates. To launch the BlackBerry Browser and access the current GPS information for your BlackBerry smartphone, complete the following steps:


1.  Set any criteria that the GPS information must follow. This can be done by creating a new          
  Criteria object and setting any of the flags that it contains. There are more flags; however, only
     two are shown below
Criteria cr = new Criteria();
cr.setAddressInfoRequired(false);
cr.setAltitudeRequired(false);

2.  Obtain an instance of the LocationProvider object and make sure it is not null.
3.  Create the thread that will retrieve the GPS information and launch the URL in the BlackBerry  
      Browser.

lp = LocationProvider.getInstance(cr);
if (lp == null) {
// No location provider, so we can't do much, so notify the user that there was a GPS error through a text field
rtField.setText(gpsError);
} else {
rtField.setText("Please wait while the local weather is
launched.");

// fire off a thread to get the GPS information and launch //the weather URL
Runnable initialGPSLoad = new Runnable() {
public void run() {
//create a new GPSInfo thread and run it
updateGPSInfo();
}
};
UiApplication.getUiApplication().invokeLater(initialGPSLoad);

}



To run the GPSInfo thread:


public void updateGPSInfo() {
Runnable gpsJob = new GPSInfo();
Thread gpsThread = new Thread(gpsJob);
gpsThread.start();
}



To obtain GPS information, the current coordinates must be retrieved. This is accomplished by using the Location and Coordinates objects.


// Get location, one minute timeout
// leave up to 180 seconds for autonomous mode
Location loc = lp.getLocation(60);
if (loc.isValid()) {
Coordinates c = loc.getQualifiedCoordinates();
if (c != null) {
// use coordinate information to update the screen
double latitude = c.getLatitude();
double longitude = c.getLongitude();
launchWeatherURL(latitude, longitude);
}
} else {
locationError("The program returned an Invalid Location"); }



For more information on launching the web browser with the desired URL, see this article desired.For more information on GPS, see this article.


/*********************************************************
* Local Weather Application
*
* Copyright 2008 Research In Motion Limited
* Created March 21, 2008
*
* This application uses the GPS capabilities of a
* BlackBerry handheld as input to the Weather services
* provided by the US National Weather Service (NOAA)
*********************************************************/
package com.rim.samples.localweather;
import javax.microedition.location.Coordinates;
import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationProvider;
import net.rim.blackberry.api.browser.Browser;
import net.rim.blackberry.api.browser.BrowserSession;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.component.Status;
import net.rim.device.api.ui.container.MainScreen;

public class LocalWeather extends UiApplication {

public static void main(String[] args) {
LocalWeather theApp = new LocalWeather();
theApp.enterEventDispatcher();
}
public LocalWeather() {
pushScreen(new localWeatherScreen());
}
}
final class localWeatherScreen extends MainScreen {

private static String appTitle = "Local Weather";
private RichTextField rtField;
public LocationProvider lp;
final String gpsError = "Unable to access GPS information.";

public localWeatherScreen() {
// Make sure the default menu is displayed on the scroll wheel
super(DEFAULT_MENU | DEFAULT_CLOSE);

// Set the title for the application
setTitle(new LabelField(appTitle, LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH));

// Create the rich text field, we'll populate it later
rtField = new RichTextField("Retrieving GPS information.");

// Add the rich text field to the screen
add(rtField);

// Now lets see if we have GPS capabilities
Criteria cr = new Criteria();
cr.setAddressInfoRequired(false);
cr.setAltitudeRequired(false);
cr.setPreferredResponseTime(Criteria.NO_REQUIREMENT);
cr.setSpeedAndCourseRequired(false);
cr.setCostAllowed(true);
cr.setHorizontalAccuracy(Criteria.NO_REQUIREMENT); cr.setPreferredPowerConsumption(Criteria.NO_REQUIREMENT);
cr.setVerticalAccuracy(Criteria.NO_REQUIREMENT);

try {
// set up a location provider instance
lp = LocationProvider.getInstance(cr);

if (lp == null) {
// No location provider, so we can't do much.
rtField.setText(gpsError);
} else {
rtField.setText("Please wait while the local weather is launched.");
// fire off a thread to get the GPS information
// and launch the weather URL
Runnable initialGPSLoad = new Runnable() {
public void run() {
updateGPSInfo();
}
}; UiApplication.getUiApplication().invokeLater(initialGPSLoad);
}
} catch (LocationException le) {
rtField.setText(gpsError);
}
}
public void makeMenu(Menu menu, int instance) {
// Add a simple About item to the menu
menu.add(mnuAbout);
};
public MenuItem mnuAbout = new MenuItem("About", 150, 10) {
// toss up a little screen with my name and the copyright
public void run() {
Status.show("Copyright 2008 Research In Motion Limited.");
}
};
public void updateGPSInfo() {
Runnable gpsJob = new GPSInfo();
Thread gpsThread = new Thread(gpsJob);
gpsThread.start();
}

public class GPSInfo implements Runnable {
public void run() {
try {
// Get location, one minute timeout
// leave up to 180 seconds for autonomous mode
Location loc = lp.getLocation(60);

if (loc.isValid()) {
Coordinates c = loc.getQualifiedCoordinates();
if (c != null) {
// use coordinate information to update //the screen
double latitude = c.getLatitude();
double longitude = c.getLongitude();
launchWeatherURL(latitude, longitude);
}
} else {
locationError("The program returned an Invalid Location");
}
} catch (InterruptedException ie) {
System.err.println(ie);
locationError("The program raised an InterruptedException.\nException: " + ie);
} catch (LocationException le) {
System.err.println(le);
locationError("The program raised a LocationException.\nException: " + le);
} catch (Exception e) {
System.err.println(e);
locationError(e.getMessage());
}
}

private void launchWeatherURL(final double latVal, final double longVal) {
//see How to - Invoke the browser
}

private void locationError(final String errMsg) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
rtField.setText(gpsError + "\n\n" + errMsg);
}
});
}
}
}


Contributors
Users Online
Currently online: 38 members 5,908 guests
Please welcome our newest community members: