Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antoniomartel/fde3ee4aa909f8ee87c871819e000a7b to your computer and use it in GitHub Desktop.
Save antoniomartel/fde3ee4aa909f8ee87c871819e000a7b to your computer and use it in GitHub Desktop.
saveAmbientData Gist
private void saveAmbientData() {
try {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 0);
Location location = getLastKnownLocation(lm);
temp.longitude = location.getLongitude();
temp.latitude = location.getLatitude();
temp.altitude = location.getAltitude();
}
else {
Location location = getLastKnownLocation(lm);
temp.longitude = location.getLongitude();
temp.latitude = location.getLatitude();
temp.altitude = location.getAltitude();
}
}
catch (Exception e) {
e.printStackTrace();
temp.longitude = 0.0;
temp.latitude = 0.0;
temp.altitude = 0.0;
}
temp.date = new Date();
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
isoFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
try {
String d = isoFormat.format(new Date());
temp.date = isoFormat.parse(d);
} catch (ParseException e) {
temp.date = null;
e.printStackTrace();
}
mTemperaturesDatabaseReference.push().setValue(temp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment