You need to use a pressure sensor to measure pressure, then use SensorManager.getAltitude (float, float) to convert the pressure of the measure along with the reference pressure of the sea level to get the height.
A hard bit is what needs to be used as a reference pressure. This will change with the weather and will drift over time. Typically, you can perform some calibration to set these values ββand possibly update them. It depends on your use case, which is the right solution.
If a correctly calibrated altitude based on pressure is much more accurate, then an altitude based on GPS, especially if you want to be interested, are altitude differences, not absolute heights. With a small number of filters on wet pressure values, you can easily detect by holding the phone at face level or the length of your arms over your head that you want to do with gps.
Ifor source share