java - 黑莓 - 开始 GPS 编码,应用程序找不到位置

标签 java blackberry gps

我正在学习使用 Blackberry 的 GPS,并且一直在遵循 Beginning Blackberry 书中处理 GPS 的部分,但代码中的某些内容是错误的,因为该应用程序只进行到“获取位置:”。我尝试从繁荣下载源代码,以防我错过了一些东西,但这也不起作用。我知道我的设备的 GPS 可以正常工作,因为我已经测试了 SDK 中的示例应用程序。下面是我的代码。

LocationHandler.java

public class LocationHandler extends Thread{
    private MyScreen screen;

    public LocationHandler(MyScreen screen){
        this.screen = screen;
    }

    public void  run(){
        Criteria criteria = new Criteria();
        criteria.setVerticalAccuracy(50);
        criteria.setHorizontalAccuracy(50);
        criteria.setCostAllowed(true);
        criteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_HIGH);

        try{
            screen.setMessage("Getting location...");
            LocationProvider provider = LocationProvider.getInstance(criteria);
            Location location = provider.getLocation(-1);

            QualifiedCoordinates qualifiedCoordinates = location.getQualifiedCoordinates();

            screen.setLocation(qualifiedCoordinates.getLongitude(), qualifiedCoordinates.getLatitude());
            String message = "Successfully got location, method: ";

            int method = location.getLocationMethod();

            if((method & Location.MTA_UNASSISTED)==Location.MTA_ASSISTED){
                message += "Unassisted GPS";
            }

            if((method & Location.MTE_CELLID)==Location.MTE_CELLID){
                message += "Cell site";
            }

            message += "\nHorizontal (Longitude) Accuracy: ";
            message += qualifiedCoordinates.getHorizontalAccuracy();

            message += "\nVertical (latitude) Accuracy: ";
            message += qualifiedCoordinates.getVerticalAccuracy();

            screen.setMessage(message);
        }catch(LocationException e){
            screen.setMessage("Location Exception: " + e.getMessage());
        }catch (InterruptedException ex){
            screen.setMessage("InteruptedException: " + ex.getMessage());
        }
    }
}

MyScreen.java

public final class MyScreen extends MainScreen
{
    private LabelField latitudeLbl;
    private LabelField longitudeLbl;
    private RichTextField messageField;

    public MyScreen()
    {        
    HorizontalFieldManager latManager = new HorizontalFieldManager();
    latManager.add(new LabelField("Latitude: "));
    latitudeLbl = new LabelField("");
    latManager.add(latitudeLbl);

    add(latManager);

    HorizontalFieldManager longManager = new HorizontalFieldManager();
    longManager.add(new LabelField("Longitude: "));
    longitudeLbl = new LabelField("");
    longManager.add(longitudeLbl);

    add(longManager);
    messageField = new RichTextField();
    add(messageField);
    }

    private void update(){
        LocationHandler handler = new LocationHandler(this);
        handler.start();
    }

    protected void makeMenu(Menu menu, int instance) {
        super.makeMenu(menu, instance);
        menu.add(new MenuItem("Update", 10, 10) {
        public void run() {
        update();
        }
        });
    }

    public void setLocation(double longitude, double latitude){
        synchronized(UiApplication.getEventLock()){
            longitudeLbl.setText(Double.toString(longitude));
            latitudeLbl.setText(Double.toString(latitude));
        }
    }

    public void setMessage(String message){
        synchronized(UiApplication.getEventLock()){
            messageField.setText(message);
        }
    }
}

最佳答案

尝试检测您是否在 LocationProvider.getInstance(criteria) 处收到 LocationException。如果是,则操作系统无法找到与您的条件 LP 匹配的内容,因此请尝试使用其他条件。

如果它确实返回有效的 LP,那么它仍然可能在 provider.getLocation(-1) 上失败。有各种异常(exception)/情况。检查API了解详情。在大多数情况下,您会收到 LocationException (如果无法检索位置或超时期限已过)或 SecurityException (如果调用应用程序没有权限)查询位置信息)。

关于java - 黑莓 - 开始 GPS 编码,应用程序找不到位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7293879/

相关文章:

blackberry - System.out.println 不会在 netbeans 控制台上打印

java - 黑莓中的布局

objective-c - 用于过滤掉无效/不准确位置的 iOS 位置服务方法

java - Hive LLAP - ORC 拆分生成失败

ssl - 我们可以在 Blackberry 中通过 TLS 客户端和 TLS 协议(protocol)为 http url 创建套接字连接吗?

java - 当您增加一个整数超过其最大值时会发生什么?

gps - 减少GPS轨迹数据丢弃冗余数据的算法?

ios - 是否可以在没有任何互联网连接/禁用蜂窝网络的情况下使用核心定位/GPS?

Java JMenuItem 在第二个menuItem之后添加边框

Java 正则表达式 : Matching Substring with newline followed by whitespace