菜鸟教程小白 发表于 2022-12-12 10:22:33

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


                                            <p><p>我正在使用 <a href="https://www.codenameone.com" rel="noreferrer noopener nofollow">codename one</a> 开发 iOS 应用程序.我想获取当前位置并通过短信发送。</p>

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

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

<pre><code>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 -&gt; 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, &#34;Provider: &#34; + provider, Toast.LENGTH_SHORT).show();
    onLocationChanged(location);
} else {
    //do something
}
</code></pre>

<p><strong>onLocationChanged 方法:</strong></p>

<pre><code>try {
    StringBuffer smsBody = new StringBuffer();
    smsBody.append(&#34;http://maps.google.com/?q=&#34;);
    smsBody.append(gpsLocation.getLatitude());
    smsBody.append(&#34;,&#34;);
    smsBody.append(gpsLocation.getLongitude());

    String phnum=&#34;xxxxx&#34;;
    String smsbod= smsBody.toString();


    Display.getInstance().sendSMS(phnum,smsbod);
} catch (IOException ex) {
    Dialog.show(&#34;Error!&#34;, &#34;Failed to start.installed?&#34;, &#34;OK&#34;, null);
    ex.printStackTrace();
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>你不能在codenameone中启动GPS,你只能检查它是否打开,如果没有打开则显示一条消息。</p>

<p>试试下面的代码:</p>

<pre><code>//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(&#34;09123456789&#34;, &#34;http://maps.google.com/?q=&#34; + lat + &#34;,&#34; + lng, false);
            } catch (IOException ex) {
                Dialog.show(&#34;Error!&#34;, &#34;Failed to start.installed?&#34;, &#34;OK&#34;, null);
                ex.printStackTrace();
            }
      } else {
            Dialog.show(&#34;GPS error&#34;, &#34;Your location could not be found, please try going outside for a better GPS signal&#34;, &#34;Ok&#34;, null);
      }
    } else {
      Dialog.show(&#34;GPS disabled&#34;, &#34;AppName needs access to GPS. Please enable GPS&#34;, &#34;Ok&#34;, 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(&#34;09123456789&#34;, &#34;http://maps.google.com/?q=&#34; + lat + &#34;,&#34; + lng, false);
      } catch (IOException ex) {
            Dialog.show(&#34;Error!&#34;, &#34;Failed to start.installed?&#34;, &#34;OK&#34;, null);
            ex.printStackTrace();
      }
    } else {
      Dialog.show(&#34;GPS error&#34;, &#34;Your location could not be found, please try going outside for a better GPS signal&#34;, &#34;Ok&#34;, null);
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于java - 代号一 GPS 提供商和当前位置,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/35122294/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/35122294/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: java - 代号一 GPS 提供商和当前位置