O'Reilly logo

Xamarin Mobile Development for Android Cookbook by Matthew Leibowitz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Listening to sensor data

Just as touching a device screen is input, so is rotating and moving the actual physical device in its environment. Most devices are built with several sensors that can be used to read the state of the device in its environment.

How to do it...

We can access all the available sensors and the sensor data on the device through the SensorManager type:

  1. The instance of the SensorManager type is obtained from the current context:
    var manager = SensorManager.FromContext(this);
  2. Once we have the manager, we check whether a particular sensor exists, and return the instance of it using the GetDefaultSensor() method:
    var type = SensorType.Accelerometer; var accelerometer = manager.GetDefaultSensor(type); if (accelerometer == null) { // ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required