I am working on an iOS app with codename one. I want to get the current location and send it by SMS.
I got this code from Java Android Studio, I don't know how to get the current location and also check if GPS is turned on.
I tried below, but without success (I'm not sure how they start the GPS and get the location)
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
}
The onLocationChanged method:
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();
}
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: