Control location tracking by using the BlackBerryLocationProvider class
You can pause, resume, and stop location tracking by using the
net.rim.device.api.gps.BlackBerryLocationProvider class.
- Import the required classes.
import net.rim.device.api.gps.*;
import javax.microedition.location.*;
- Create a new class and a constructor.
public class handleGPS
{
static BlackBerryLocationProvider myProvider;
public handleGPS()
{
}
}
- In the constructor, create a try/catch block. In the block, create an instance of the BlackBerryCriteria class by passing the GPS mode as a parameter to the constructor.
try
{
BlackBerryCriteria myCriteria =
new BlackBerryCriteria(GPSInfo.GPS_MODE_AUTONOMOUS);
}
catch ( UnsupportedOperationException uoex )
{
return;
}
- In the try part of the block, create a new try/catch block.
In this block, create an instance of the BlackBerryLocationProvider
class by retrieving an instance of the BlackBerryCriteria
class. Invoke setLocationListener() by passing the interval value, timeout value, and maximum age as parameters to add a LocationListener.
try
{
myProvider = (BlackBerryLocationProvider)
LocationProvider.getInstance(myCriteria);
myProvider.setLocationListener(new handleGPSListener(), 10, -1, -1);
}
catch ( LocationException lex )
{
return;
}
myProvider.pauseLocationTracking(30);
myProvider.resumeLocationTracking();
myProvider.stopLocationTracking();
- Outside of the try/catch
block, invoke pauseLocationTracking(), resumeLocationTracking(), or stopLocationTracking() to pause, resume, or stop location tracking.
myProvider.pauseLocationTracking(30);
myProvider.resumeLocationTracking();
myProvider.stopLocationTracking();
- In the class, implement the LocationListener interface. Implement the basic framework
for the locationUpdated() method, and the providerStateChanged() method.
public static class handleGPSListener implements LocationListener
{
public void locationUpdated(LocationProvider provider, Location location)
{
if (location.isValid())
{}
else
{}
}
public void providerStateChanged(LocationProvider provider, int newState)
{
if (newState == LocationProvider.AVAILABLE)
{}
else if (newState == LocationProvider.OUT_OF_SERVICE)
{}
else if (newState == LocationProvider.TEMPORARILY_UNAVAILABLE )
{}
}
}
Was this information helpful? Send us your comments.