Change the screen light level pending on sensor data

Hello I'm new in this app, I try to create an app to have the screen becoming black when the phone moves and come back to transparency one i stop moving.
in other words, i want to be be able to use my phone only when i stay still with no movement so it's a kind of nudge i want to create that makes the phone not usable when i walk or shake it.
I know how to detect the movement but i don't see how to ;
*have the app running backround.
*How to change the screen contrast /luminosity pending on the sensor movement value.
Any Idea? .
Thank you for your help

Maybe something like this?