How to understand that the screen is blocked well or extinguished?

  • @asdasdasd, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Nicolas Chabanovsky

1 answer 1

To do this, there are special intentions. Intent.ACTION_SCREEN_OFF & Intent.ACTION_SCREEN_ON. In order for your application to receive these intentions, you must specify an IntentFilter for your activation. To do this, you can inherit from the class BroadcastReceiver. Here is a good example from the Internet. A source

 public class ScreenReceiver extends BroadcastReceiver { // THANKS JASON public static boolean wasScreenOn = true; @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { // DO WHATEVER YOU NEED TO DO HERE wasScreenOn = false; } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { // AND DO WHATEVER YOU NEED TO DO HERE wasScreenOn = true; } } } 

Then we set an intent filter for your activity.

 public class ExampleActivity extends Activity { @Override protected void onCreate() { // INITIALIZE RECEIVER IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); BroadcastReceiver mReceiver = new ScreenReceiver(); registerReceiver(mReceiver, filter); // YOUR CODE } @Override protected void onPause() { // WHEN THE SCREEN IS ABOUT TO TURN OFF if (ScreenReceiver.wasScreenOn) { // THIS IS THE CASE WHEN ONPAUSE() IS CALLED BY THE SYSTEM DUE TO A SCREEN STATE CHANGE System.out.println("SCREEN TURNED OFF"); } else { // THIS IS WHEN ONPAUSE() IS CALLED WHEN THE SCREEN STATE HAS NOT CHANGED } super.onPause(); } @Override protected void onResume() { // ONLY WHEN SCREEN TURNS ON if (!ScreenReceiver.wasScreenOn) { // THIS IS WHEN ONRESUME() IS CALLED DUE TO A SCREEN STATE CHANGE System.out.println("SCREEN TURNED ON"); } else { // THIS IS WHEN ONRESUME() IS CALLED WHEN THE SCREEN STATE HAS NOT CHANGED } super.onResume(); } } 

EDIT

 @Override protected void onPause() { super.onPause(); PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE); boolean isScreenOn = powerManager.isScreenOn(); if (!isScreenOn) { // Делайте что Вам надо если потух экран. } } 

And for inclusion

 @Override protected void onResume() { super.onResume(); PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE); boolean isScreenOn = powerManager.isScreenOn(); if (isScreenOn) { // Делайте что Вам надо если включен экран. } } 

Decide what suits you best.

  • How is that all too much you do not think ?. And besides, perhaps it’s not as bad as using WindowsManager to implement it ... And besides, I use the Service - asdasdasd
  • Many are not bad, read about the Broadcast receiver. In one way or another, you will need to receive intents from the system. You need to register your receiver in the intent filter of your activation. You can also do this with PowerManager. See the update response - CROSP
  • I solved the problem in a different way .... I don’t know why, but you could write that when blocking, Pause or Resume is called .... That's the whole problem. all topic can be covered - asdasdasd
  • You read carefully what methods I have overridden in Activiti? - CROSP