我一直致力于获取 GPS 位置,我已经阅读了许多示例和文章,但还没有取得任何成功。可能是我遗漏了一些非常小的东西,但我是 BB 开发的新手。
下面是代码
package mypackage;
import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationProvider;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
public final class MyScreen extends MainScreen {
/**
* Creates a new MyScreen object
*/
public MyScreen() {
setTitle("GPS Demo");
new GpsThread().start();
}
public boolean onClose() {
UiApplication.getUiApplication().requestBackground();
return false;
}
private class GpsThread extends Thread {
Location currentLoc; // Stores component information of our position
Criteria cr; // Settings for the GPS - we can read it at
// different accuracy levels
LocationProvider lp; // LocationProvider does the actual work of reading
// coordinates from the GPS
public GpsThread() {
add(new LabelField(("GPS Constructor")));
cr = new Criteria();
}
public void run() {
add(new LabelField(("run")));
showLocationToast();
}
private void setCriteria() {
// I basically set no requirements on any of the horizontal,
// vertical,
// or
// power consumption requirements below.
// The distance components are set in meters if you do want to
// establish
// accuracy - the less the accuracy, the
// quicker and more likely a successful read (I believe).
// You can also set power consumption, between low, medium, high (or
// no
// requirement)
// There are also a number of other settings you can tweak such as
// minimum
// response time, if altitude is required,
// speed required, etc. It all depends on the exact application
// you're
// writing and how specific you need the info to
cr.setCostAllowed(true);
cr.setHorizontalAccuracy(Criteria.NO_REQUIREMENT);
cr.setVerticalAccuracy(Criteria.NO_REQUIREMENT);
cr.setPreferredPowerConsumption(Criteria.POWER_USAGE_HIGH);
add(new LabelField(("Criteria set ")));
}
private void getLocation() throws InterruptedException {
setCriteria();
try {
add(new LabelField(("getting location")));
// Get a new instance of the location provider using the
// criteria we
// established above.
if (lp != null) {
lp.setLocationListener(null, 0, -1, -1);
lp.reset();
lp = null;
}
add(new LabelField(("lp had something so resetting it")));
lp = LocationProvider.getInstance(cr);
add(new LabelField(("got locationprovider instance ... ")));
// Now populate our location object with our current location
// (with
// a 60 second timeout)
lp.setLocationListener(new MyLocationListener(), 120, -1, -1);
currentLoc = lp.getLocation(60);
String location = "Latitude : "
+ currentLoc.getQualifiedCoordinates().getLatitude()
+ "\n Longitude : "
+ currentLoc.getQualifiedCoordinates().getLongitude();
add(new LabelField((location)));
}
// If we hit the timeout or encountered some other error, report it.
catch (LocationException e) {
// Dialog.alert("Error getting coordinates");
add(new LabelField(e.getMessage()));
return;
}
}
private void showLocationToast() {
add(new LabelField(("showing toast")));
try {
getLocation();
} catch (InterruptedException e) {
add(new LabelField(e.getMessage()));
}
add(new LabelField(("Got all info")));
String location = "Latitude : "
+ currentLoc.getQualifiedCoordinates().getLatitude()
+ "\n Longitude : "
+ currentLoc.getQualifiedCoordinates().getLongitude();
add(new LabelField("Showing toast..." + location));
}
}
}
附加信息: 我的听众类只有在更新位置时必须显示的对话框。我什至尝试在不使用监听器的情况下获取单个位置
喜欢
lp.getLocation (60);
就在我设置监听器的位置之前。但是两次我都只在我设置监听器的地方得到标签。我将使用 Timer
和 TimerTask
但是我能够正确完成这个演示。
我正在使用 9900 进行开发,该应用程序应该在 4.5.0 上
最佳答案
我看到两件事对我来说不合适。我无法复制您的确切测试场景,所以我不能肯定地说它们就是问题所在……只是它们看起来很可疑。
线程
您正在从后台线程(您的 GpsThread
)调用更新您的 UI,添加 LabelFields
。我认为那是不对的。通常,当我有一个需要更新 UI 的后台线程时,我总是这样做
private void addNewLabelField(final String text) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
add(new LabelField(text));
}
});
}
然后,您可以从任何地方调用 addNewLabelField("something");
。
也就是说,如果您尝试这样做,我认为您会得到一个 IllegalStateException
,并且您说您没有得到任何异常。无论如何,我认为将与 UI 相关的调用保持在主 (UI) 线程上仍然是一种很好的做法。
位置标准
您正在使用NO_REQUIREMENT、NO_REQUIREMENT、允许的成本、POWER_USAGE_HIGH作为您的位置标准
。如果我查看此文档:
http://supportforums.blackberry.com/t5/Java-Development/Location-APIs-Start-to-finish/ta-p/571949
...向下滚动到名为JSR-179 API 的Criteria mapping 的表,这似乎暗示只有CDMA 手机才支持这种精确定位条件。但是,according to this , 9900 是 GSM 电话。您知道吗(也许我错了,它也出现在 CDMA 型号中)?
但是,如果您碰巧选择了手机不支持的 Criteria
,您可能无法获得位置结果。因此,我肯定会尝试另一种标准组合,这些组合在我上面链接的文档中列出。
这是我最好的猜测。
关于blackberry - 没有获取 GPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11219494/