Created
January 7, 2018 19:00
-
-
Save antoniomartel/fde3ee4aa909f8ee87c871819e000a7b to your computer and use it in GitHub Desktop.
saveAmbientData Gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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