我一直在努力获取 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 上