我编写了一个非常简单的应用程序,它向 LocationProvider 询问位置并将其打印到 System.out。这在模拟器中效果很好。但是,当我在我的黑莓设备上运行它时,对 getLocation 的调用似乎无限期挂起。我在一个单独的线程中运行代码,该线程只是获取提供者并询问位置。我尝试使用空标准(应该给我默认值对吗?)以及应该提供辅助然后自治的标准。我在下面包含了我的代码。当我在我的设备上运行它时,它会挂起对 getLocation 的调用。下面是我的代码..plzz 告诉我可能做错了什么......
public void getLocation() {
Thread t = new Thread(new Runnable() {
private double lat;
private double lon;
public void run() {
Criteria cr = new Criteria();
cr.setHorizontalAccuracy(Criteria.NO_REQUIREMENT);
cr.setVerticalAccuracy(Criteria.NO_REQUIREMENT);
cr.setCostAllowed(false);
cr.setPreferredPowerConsumption(Criteria.NO_REQUIREMENT);
cr.setPreferredResponseTime(1000);
LocationProvider lp = null;
try {
lp = LocationProvider.getInstance(cr);
} catch (LocationException e) {
// System.out.println("*****************Exception" + e);
}
Coordinates c = null;
if (lp == null) {
UiApplication.getUiApplication().invokeLater(
new Runnable() {
public void run() {
Dialog.alert("GPS not supported!");
return;
}
});
} else {
// System.out.println("OK");
switch (lp.getState()) {
case LocationProvider.AVAILABLE:
// System.out.println("Provider is AVAILABLE");
Location l = null;
try {
l = lp.getLocation(-1);
} catch (LocationException e) {
// System.out.println("***Location Exception caught "
// + e);
} catch (InterruptedException e) {
// System.out.println("***Interrupted Exception aught:"
// + e);
} catch (Exception e) {
// System.out.println("***Exception caught :"
// ;+ e);
}
if (l != null && l.isValid()) {
c = l.getQualifiedCoordinates();
}
if (c != null) {
lat = c.getLatitude();
lon = c.getLongitude();
System.out.println(lat + " - " + lon);
}
}
}
}
});
t.start();
}