'How to make the screen go dark when a proximity sensor is detected on Android?

I have an application for calls, when the user enters the call page, there is a voice, etc., I want to implement a proximity sensor, so that when the user brings the phone to his ear, the screen is inactive and darkened. Here is my code for the sensor (it works).

    using Android.App;
    using Android.Content;
    using Android.Content.PM;
    using Android.Hardware;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Corporate_messenger.DB;
    using Corporate_messenger.Service;
    using Corporate_messenger.Service.Notification;
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Xamarin.Forms;
    
    namespace Corporate_messenger.Droid
    {
        [Activity(Label = "CallActivity",ScreenOrientation = ScreenOrientation.Portrait)]
        public class CallActivity : Activity, ISensorEventListener
        {
    
            Android.Widget.Button BtnStartCall;
            Android.Widget.Button BtnEndCall;
            Android.Widget.Button BtnEndCallCenter;
    
            SensorManager sensorManager;
            Sensor proximitySensor;
    
      
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
     
                    sensorManager = (SensorManager) GetSystemService(Context.SensorService);
                    sensorManager.RegisterListener(this, sensorManager.GetDefaultSensor(SensorType.Proximity), SensorDelay.Ui);
                    proximitySensor = sensorManager.GetDefaultSensor(SensorType.Proximity);
            }
    
          
    
            public void OnAccuracyChanged(Sensor sensor, [GeneratedEnum] SensorStatus accuracy)
            {
                var s = sensor;
            }
    
            public void OnSensorChanged(SensorEvent e)
            {
              
// The sensor is triggered here, at this moment I can’t figure out how to Hide the screen (do not
               // active , darken)
            }
        }
    }

Please help me to make the screen not active "Dimed"



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source