我一直致力于获取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));
}
}
}
其他信息: class my listener只有在更新位置时必须显示的对话框。我甚至试图在不使用监听器的情况下获得单一位置
像
lp.getLocation (60);
就在我设置我的听众之前。但是这两次我只把Label放到我设置监听器的地方。我将使用Timer
和TimerTask
,但我可以正确使用此演示。
我使用9900进行开发,应用程序应该是4.5.0
答案 0 :(得分:3)
我看到两件看起来不合适的事情。我不能复制你的确切测试场景,所以我不能肯定他们是问题...只是他们看起来很可疑。
您正在通过后台线程(您的LabelFields
)调用更新您的用户界面,添加GpsThread
。我不认为这是对的。一般来说,当我有一个需要更新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 作为您的位置Criteria
。如果我看一下这个文件:
http://supportforums.blackberry.com/t5/Java-Development/Location-APIs-Start-to-finish/ta-p/571949
...向下滚动到名为 JSR-179 API的标准映射的表格,似乎暗示这个精确的位置标准仅支持CDMA手机。但是,according to this,9900是GSM手机。你知道吗(也许我错了,它也有CDMA模型)?
但是,如果您碰巧选择了手机不支持的Criteria
,您可能无法获得位置结果。所以,我肯定会尝试另一种标准组合,这些标准列在我上面链接的文件中。
这是我最好的猜测。