Codename one GPS provider and current location

十年热恋 提交于 2019-12-04 18:27:39

You can't start the GPS in codenameone, you can only check if it's turned on and display a message if it's not.

Try the code below:

//Check if location is turned on and your app is allowed to use it.
if (Display.getInstance().getLocationManager().isGPSDetectionSupported()) {
    if (Display.getInstance().getLocationManager().isGPSEnabled()) {
        InfiniteProgress ip = new InfiniteProgress();
        final Dialog ipDlg = ip.showInifiniteBlocking();
        //Cancel after 20 seconds
        Location loc = LocationManager.getLocationManager().getCurrentLocationSync(20000);
        ipDlg.dispose();
        if (loc != null) {
            double lat = loc.getLatitude();
            double lng = loc.getLongitude();
            try {
                Display.getInstance().sendSMS("09123456789", "http://maps.google.com/?q=" + lat + "," + lng, false);
            } catch (IOException ex) {
                Dialog.show("Error!", "Failed to start.  installed?", "OK", null);
                ex.printStackTrace();
            }
        } else {
            Dialog.show("GPS error", "Your location could not be found, please try going outside for a better GPS signal", "Ok", null);
        }
    } else {
        Dialog.show("GPS disabled", "AppName needs access to GPS. Please enable GPS", "Ok", null);
    }
} else {
    InfiniteProgress ip = new InfiniteProgress();
    final Dialog ipDlg = ip.showInifiniteBlocking();
    //Cancel after 20 seconds
    Location loc = LocationManager.getLocationManager().getCurrentLocationSync(20000);
    ipDlg.dispose();
    if (loc != null) {
        double lat = loc.getLatitude();
        double lng = loc.getLongitude();
        try {
            Display.getInstance().sendSMS("09123456789", "http://maps.google.com/?q=" + lat + "," + lng, false);
        } catch (IOException ex) {
            Dialog.show("Error!", "Failed to start.  installed?", "OK", null);
            ex.printStackTrace();
        }
    } else {
        Dialog.show("GPS error", "Your location could not be found, please try going outside for a better GPS signal", "Ok", null);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!