In this recipe, we will discuss how to use the proximity sensor to disable the device screen.
Create a new Single View Application in Xamarin Studio and name it ProximitySensorApp
.
For this project, no controls are needed on the view controller. Declare an
NSObject
field that will hold the notification observer by using the following command:private NSObject proximityObserver;
Override the
ViewWillAppear
method of the controller and implement it according to the following code:public override void ViewWillAppear (bool animated) { base.ViewWillAppear (animated); UIDevice.CurrentDevice.ProximityMonitoringEnabled = true; if (UIDevice.CurrentDevice.ProximityMonitoringEnabled) { this.proximityObserver = UIDevice.Notifications.ObserveProximityStateDidChange((s, e) => { Console.WriteLine("Proximity state: {0}", UIDevice.CurrentDevice.ProximityState)...