FLAG_TURN_SCREEN_ON does not always work

i start the action from BroadcastReceiver, which is triggered by the alaram (type RTC_WAKEUP). in onCreating this action I am adding these flags

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON ); 
Problem

lies in the fact that sometimes (approximately 10% of cases) the screen does not turn on. (a notification signal sounds here, which also starts in the onReceive () receiver). Then, if I press the phone’s power button, the screen turns on, my activity is shown and it turns off instantly. that the power button works well. This happens on android 2.3.7, and here is the onReceive () method

 @Override public void onReceive(Context context, Intent intent) { m_Context = context; Bundle extras = intent.getExtras(); final int id = extras.getInt("timer_id"); Intent activityIntent = new Intent(m_Context, MyActivity.class); activityIntent.putExtra("timer_id", id); activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); m_Context.startActivity(activityIntent); // and now load the alarm sound and play it for the desired time showFinishedNotification(); } 

I would like to avoid using PowerManager as it needs permission, and flags are preferred.

What could be the problem? logcat does not cause any problems ...

+6
source share
3 answers

I'm a little late to the party here, but I struggled with this for a while and finally found a way to make the screen unlock every time. I am adding flags to the onAttachToWindow () event. I usually do this from the WakefulBroadcastReceiver, so the screen goes smoothly, but depends on usage.

 @Override public void onAttachedToWindow() { super.onAttachedToWindow(); //Screen On getWindow().addFlags( WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); } private void clearFlags() { //Don't forget to clear the flags at some point in time. getWindow().clearFlags( WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); } 
+5
source

From my experience and research on this topic:

  • FLAG_TURN_SCREEN_ON cannot be used to turn the screen on and off several times in your application.

  • FLAG_TURN_SCREEN_ON can only be used once to turn on the screen when creating a new action (preferably in the onCreate () method) or when re-creating the view .

Now you can get around this limitation:

  • Starting a new action and setting a flag there, and then completing the action (user or programmatic) so that the screen turns off.
  • Setting params.screenBrightness to as "dim" as possible , sometimes the screen "displays OFF". Then you can increase the brightness to β€œturn on” the screen. However, this often does not work, because the screen is still dull, but visible, also it does not work if the user locks the phone.
  • Using Power Manager Wakelock (this still works, but Android does not support this feature, so they do not recommend using this technique). However, as far as I can tell, this is the only way to get my application to reliably turn on / off the screen.

None of them are perfect (they actually feel like hacks), but just use the one that works best for your applications.

You can read here:

+5
source

the problem is that sometimes (approximately 10% of cases) the screen does not turn on

If I had to guess, the device returns to sleep before starting work. As soon as onReceive() returns, the device can and will sleep, and some time after onReceive() returns, before your activity begins, some time will happen.

In the same scenario, but replacing startActivity() with startService() , I had to write a WakefulIntentService that uses WakeLock so that the device remains long enough for it to do its job, then releases WakeLock .

+3
source

Source: https://habr.com/ru/post/912130/


All Articles