aF.
aF.

Reputation: 66687

How to discover if the screen is ON or OFF while developing in Android?

How can I know if the screen is ON/OFF?

Upvotes: 2

Views: 816

Answers (1)

FoamyGuy
FoamyGuy

Reputation: 46846

If you simply wish to poll the current state you can do so this way.

pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
boolean screen = pm.isScreenOn();

If you are looking to be notified when the state changes you'll have to register a Broadcast Reciver to listen for ACTION_SCREEN_OFF and ACTION_SCREEN_ON. For this particular intent you must set your Intent Filter from java code. Doing so in the Manifest does not seem to work.

Upvotes: 10

Related Questions