Blackberry - Beginning GPS coding, app won't find location
I'm learning to use thr Blackberry's GPS and have been following the section of the Beginning Blackberry book dealing with GPS but something in the code is wrong as the app only goes as far as 'Getting location: '. I tried downloading the source from the boom incase I missed something but that didn't work either. I know my device's GPS works as I've tested the sample app from the SDK. Below is my code.
LocationHandle开发者_JS百科r.java
public class LocationHandler extends Thread{
private MyScreen screen;
public LocationHandler(MyScreen screen){
this.screen = screen;
}
public void run(){
Criteria criteria = new Criteria();
criteria.setVerticalAccuracy(50);
criteria.setHorizontalAccuracy(50);
criteria.setCostAllowed(true);
criteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_HIGH);
try{
screen.setMessage("Getting location...");
LocationProvider provider = LocationProvider.getInstance(criteria);
Location location = provider.getLocation(-1);
QualifiedCoordinates qualifiedCoordinates = location.getQualifiedCoordinates();
screen.setLocation(qualifiedCoordinates.getLongitude(), qualifiedCoordinates.getLatitude());
String message = "Successfully got location, method: ";
int method = location.getLocationMethod();
if((method & Location.MTA_UNASSISTED)==Location.MTA_ASSISTED){
message += "Unassisted GPS";
}
if((method & Location.MTE_CELLID)==Location.MTE_CELLID){
message += "Cell site";
}
message += "\nHorizontal (Longitude) Accuracy: ";
message += qualifiedCoordinates.getHorizontalAccuracy();
message += "\nVertical (latitude) Accuracy: ";
message += qualifiedCoordinates.getVerticalAccuracy();
screen.setMessage(message);
}catch(LocationException e){
screen.setMessage("Location Exception: " + e.getMessage());
}catch (InterruptedException ex){
screen.setMessage("InteruptedException: " + ex.getMessage());
}
}
}
MyScreen.java
public final class MyScreen extends MainScreen
{
private LabelField latitudeLbl;
private LabelField longitudeLbl;
private RichTextField messageField;
public MyScreen()
{
HorizontalFieldManager latManager = new HorizontalFieldManager();
latManager.add(new LabelField("Latitude: "));
latitudeLbl = new LabelField("");
latManager.add(latitudeLbl);
add(latManager);
HorizontalFieldManager longManager = new HorizontalFieldManager();
longManager.add(new LabelField("Longitude: "));
longitudeLbl = new LabelField("");
longManager.add(longitudeLbl);
add(longManager);
messageField = new RichTextField();
add(messageField);
}
private void update(){
LocationHandler handler = new LocationHandler(this);
handler.start();
}
protected void makeMenu(Menu menu, int instance) {
super.makeMenu(menu, instance);
menu.add(new MenuItem("Update", 10, 10) {
public void run() {
update();
}
});
}
public void setLocation(double longitude, double latitude){
synchronized(UiApplication.getEventLock()){
longitudeLbl.setText(Double.toString(longitude));
latitudeLbl.setText(Double.toString(latitude));
}
}
public void setMessage(String message){
synchronized(UiApplication.getEventLock()){
messageField.setText(message);
}
}
}
Try to detect whether you get a LocationException
at LocationProvider.getInstance(criteria)
. If yes, then OS fails to find matching to your criteria LP, so try to use another criteria.
If it does return a valid LP, then it still may fail on provider.getLocation(-1)
. There are various exceptions/cases. Check the API for details. In most cases here you get either LocationException
(if the location couldn't be retrieved or if the timeout period expired) or SecurityException
(if the calling application does not have a permission to query the location information).
精彩评论