黑莓 - LocationProvider.getLocation() 挂起

标签 blackberry gps jsr179

我编写了一个非常简单的应用程序,它向 LocationProvider 询问位置并将其打印到 System.out。这在模拟器中效果很好。但是,当我在我的黑莓设备上运行它时,对 getLocation 的调用似乎无限期挂起。我在一个单独的线程中运行代码,该线程仅获取提供者并询问位置。我尝试使用 null Criteria(它应该给我默认值吗?)以及一个应该提供 Assist 然后是 Autonomous 的 Criteria。我在下面包含了我的代码。当我在我的设备上运行它时,它卡在对 getLocation 的调用上。下面是我的代码......请告诉我我可能做错了什么......

public void getLocation() {
    Thread t = new Thread(new Runnable() {
        private double lat;
        private double lon;
        public void run() {
            Criteria cr = new Criteria();
            cr.setHorizontalAccuracy(Criteria.NO_REQUIREMENT);
            cr.setVerticalAccuracy(Criteria.NO_REQUIREMENT);
            cr.setCostAllowed(false);
            cr.setPreferredPowerConsumption(Criteria.NO_REQUIREMENT);
            cr.setPreferredResponseTime(1000);
            LocationProvider lp = null;
            try {
                lp = LocationProvider.getInstance(cr);
            } catch (LocationException e) {
            // System.out.println("*****************Exception" + e);
            }
            Coordinates c = null;
            if (lp == null) {
                UiApplication.getUiApplication().invokeLater(
                    new Runnable() {
                    public void run() {
                        Dialog.alert("GPS not supported!");
                        return;
                    }
                    });
            } else {
                // System.out.println("OK");
                switch (lp.getState()) {
                case LocationProvider.AVAILABLE:
                // System.out.println("Provider is AVAILABLE");
                        Location l = null;
                        try {
                        l = lp.getLocation(-1);
                    } catch (LocationException e) {
                    // System.out.println("***Location Exception caught "
                    // + e);
                    } catch (InterruptedException e) {
                    // System.out.println("***Interrupted Exception aught:"
                    // + e);
                    } catch (Exception e) {
                    // System.out.println("***Exception caught :"
                    // ;+ e);
                    }
                        if (l != null && l.isValid()) {
                        c = l.getQualifiedCoordinates();
                    }
                    if (c != null) {
                            lat = c.getLatitude();
                        lon = c.getLongitude();
                        System.out.println(lat + "  -  " + lon);
                    }
                }
            }
        }
    });
    t.start();
}

最佳答案

尝试:


谈论挂起的线程,

locationProvider.getLocation(-1) 

挂起是因为如果您使用 -1,则不会超时。试试这个:

    int timeout = 120;
    try {
        location = provider.getLocation(timeout);
    } catch (LocationException e) {
        System.out.println("Location timeout");
    } catch (InterruptedException e) {
        System.out.println("InterruptedException"+e.getMessage());
    }

此外,如果您在模拟器中使用System.out.println(text),在设备上最好

getApplication().invokeLater(new Runnable(){
    public void run() {
        screen.add(new LabelField(text));           
    }
});

getApplication().invokeLater(new Runnable(){
    public void run() {
        Dialog.inform(text);            
    }
});

关于黑莓 - LocationProvider.getLocation() 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1536742/

相关文章:

blackberry - 如何在 Blackberry MapField 中显示多个位置?

blackberry - 如何在 BlackBerry 中找到两地之间的路线?

java - BlackBerry 数学实用程序(Pow & Round)

android - Android 中更快的 GPS 定位

python - 两点之间的方位

angularjs - ionic : Reverse Geocoding

java - BlackBerry 中的“+”字符串运算符和 StringBuilder

blackberry - 如何处理这个错误? "Graphics acceleration error - BlackBerry 9900 Simulator"

jquery - 为移动设备禁用 HTML 中的拼写帮助