• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

java - 代号一 GPS 提供商和当前位置

[复制链接]
菜鸟教程小白 发表于 2022-12-12 10:22:33 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在使用 codename one 开发 iOS 应用程序.我想获取当前位置并通过短信发送。

我从 Java Android Studio 得到这段代码,我不知道如何获取当前位置以及检查 GPS 是否打开。

我在下面尝试过,但没有成功(我不确定他们如何启动 GPS 并获取位置)

LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabledGPS = service.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabledGPS) {
   //alert GPS is off
}
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the location provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);

// Initialize the location fields
if (location != null) {
    Toast.makeText(this, "rovider: " + provider, Toast.LENGTH_SHORT).show();
    onLocationChanged(location);
} else {
    //do something
}

onLocationChanged 方法:

try {
    StringBuffer smsBody = new StringBuffer();
    smsBody.append("http://maps.google.com/?q=");
    smsBody.append(gpsLocation.getLatitude());
    smsBody.append(",");
    smsBody.append(gpsLocation.getLongitude());

    String phnum="xxxxx";
    String smsbod= smsBody.toString();


    Display.getInstance().sendSMS(phnum,smsbod);
} catch (IOException ex) {
    Dialog.show("Error!", "Failed to start.  installed?", "OK", null);
    ex.printStackTrace();
}



Best Answer-推荐答案


你不能在codenameone中启动GPS,你只能检查它是否打开,如果没有打开则显示一条消息。

试试下面的代码:

//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);
    }
}

关于java - 代号一 GPS 提供商和当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35122294/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap