I am trying to get the values of the magnetic field and the light intensity using Android's SensorManager
. So, I searched online, and found out that I should use
mySensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
myCompassSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
for the magnetic field sensor, and
mySensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
myCompassSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
for the light sensor. However, I don't want continuous measurement of the magnetic field strength and light intensity. I just want to obtain the current measurements on button press, so that I can store these values in a database. The codes and examples I found take continuous measurements by using registerlistener
, but as mentioned, this is not what I want. Any help would be appreciated.
What you can do is to register your listener in the button click event "as you mention you showed in many examples" and after taking the first value of light or magnetic then unregister the listener for light or magnetic respectively. I have done something similar for the magnetic but with a counter which unregisters the listener after a number of measurements. So, you can have a look:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.