3

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

就在我设置听众的地方之前。但是这两次我都只得到了我设置监听器的标签。我将使用TimerTimerTask但是我能够正确地进行此演示。

我正在使用 9900 进行开发,该应用程序应该在 4.5.0 上

4

1 回答 1

3

我看到了两件对我来说看起来不正确的事情。我无法复制您的确切测试场景,所以我不能肯定它们是问题所在……只是它们看起来很可疑。

穿线

您正在LabelFields从后台线程(您的GpsThread)调用更新您的 UI,并添加 。我不认为这是对的。一般来说,当我有一个需要更新 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_HIGHCriteria。如果我看这个文件:

http://supportforums.blackberry.com/t5/Java-Development/Location-APIs-Start-to-finish/ta-p/571949

...向下滚动到名为Criteria mapping for JSR-179 API的表,这似乎暗示此精确位置标准仅支持 CDMA 手机。但是,据此,9900 是一款 GSM 手机。你知道吗(也许我错了,它也有 CDMA 型号)?

但是,如果您碰巧选择了Criteria手机不支持的手机,您可能无法获得定位结果。所以,我肯定会尝试另一种标准组合,这些标准列在我上面链接到的那个文件中。

That's my best guess.

于 2012-06-27T09:34:28.670 回答