Hhow to detect when user lock and unlock his device

Hello, i created my game and i have a problem, when the player lock the screen by shortly pressing the Hardware power key. The game is still running when the device is locked => The game should be paused when the device is locked. The player press the Hardware power key again to unlock the device, at this time the game is over after unlocking the device => The game sould be resume normally after unlocking the device.

So my question is how to detect when user lock device to paused game and when user unlock the device to resumed game.

Can any one help me?