私は多くの例や記事を調べてきましたが、まだ成功していません. 私が推測する非常にマイナーなものが欠けているかもしれませんが、私は 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 にあるはずです