2016-02-01 30 views
2

我正在與一個iOS應用程序codename one。我想獲取當前位置並通過短信發送。Codename一個GPS提供商和當前位置

我從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, "Provider: " + 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(); 
} 

回答

3

你可以用」在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); 
    } 
} 
+0

在這個時候,我不接受錯誤,但沒有happend。不是短信發送,而不是對話框警告。我打開GPS –

+0

在哪個設備上試用?嘗試更改此Display.getInstance()。sendSMS(「09123456789」,「http://maps.google.com/?q=」+ lat +「,」+ lng,false);'Display this.getInstance ().sendSMS(「09123456789」,「http://maps.google.com/?q=」+ lat +「,」+ lng,true);' – Diamond

+0

我正在使用三星Xcover 2(android)應用程序它應該在IOS上工作,但我認爲我可以在任何設備上先測試它 –

相關問題