If you have an Android of any brand like Samsung, Motorola, Xiaomi, Realme or any phone and your screen getting black then it means your Proximity sensor is not working correctly. First detect the issue by installing the Proximity sensor test.
If your phone failed the proximity sensor test then their may be some chances that still it will work.
Remove screen protector
Some time your screen and glass protector are conflicting with proximity sensor. Remove it and test again to check if working.
Press screen from top
Usually your phone LCD glass glue get weak and it leave some space that sometime stop proximity sensor from working. Press from top will may fix it.
Clean your LCD near sensor
Try to locate the proximity sensor of your phone and you can search on google if you unable to find. Clean that place and test again.
Reset Proximity Sensor setting
Use Proximity sensor reset app to over ride the proximity setting. Restart the phone and check if its start to work.
Disable proximity sensor
Disable proximity sensor if not working. Every brand have different setting search for your phone settings.
Replace the Proximity Sensor
If above tips unable to fix the issue then you have just option to replace the sensor or claim the warranty.