FLAG_TURN_SCREEN_ON并非始终有效

问题描述:

我从BroadcastReceiver启动一个活动,该活动由一个alaram(RTC_WAKEUP类型)触发.在该活动的onCreate中,我添加了这些标志

i start an activity from a BroadcastReceiver, which is triggered by an alaram (RTC_WAKEUP type). in onCreate of that activity i add 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        
);

问题是有时(大约10%的情况)屏幕无法打开.警报已正确触发(在这里是通知的声音,也在接收器的onReceive()中触发.然后,如果我按了电话的电源按钮,则屏幕将打开,显示我的活动并立即关闭.那,电源按钮就可以了.这发生在android 2.3.7上,这是onReceive()方法

problem is that sometimes (approximately 10% cases) the screen does not turn on. the alarm is correctly triggered (i here the sound of a notification, which is also fired in the receiver's onReceive(). then, if i hit the phone's power button, the screen turns on, showing my activity, and instantly turns off. after that, the power button works good. this happen 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();
}

我想避免使用PowerManager,因为它需要权限,并且标志是首选方式.

i would like to avoid using PowerManager, as it needs a permission, and the flags are the prefered way.

可能是什么问题? logcat没有显示任何问题...

what could be a problem? logcat does not show any problems...

根据我的经验和对该主题的研究:

From my experience and research on this topic:

  • FLAG_TURN_SCREEN_ON 不能用于在应用程序中多次打开和关闭屏幕.

  • The FLAG_TURN_SCREEN_ON can not be used to turn the screen ON and OFF multiple times in your application.

FLAG_TURN_SCREEN_ON 仅在重新创建视图时.

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

现在,您可以通过以下方法解决此限制:

Now, you can get around this limitation by:

  • 启动一个新活动并在其中设置标志,然后(由用户或以编程方式)结束该活动以关闭屏幕.
  • params.screenBrightness 参数设置为尽可能暗淡" ,有时屏幕出现关闭".然后,您可以增加亮度以打开"屏幕.但是,这通常不起作用,因为屏幕仍然暗淡但可见,如果用户锁定手机,这也将不起作用.
  • 使用电源管理器唤醒锁(这仍然有效,但是Android不推荐使用此功能,因此他们不鼓励使用此技术.但是,据我所知,这是使我的应用程序可靠地打开/关闭屏幕的唯一方法.
  • Launching a new activity and setting the flag there, then finishing the activity (by the user or programmatically) to let the screen turn off.
  • Setting the params.screenBrightness parameters to as "dim" as possible, sometimes the screen "appears OFF". You can then increase the brightness to "turn ON" the screen. However, this often does not work as the screen is still dim but visible, also this doesn't work if the user locks the phone.
  • Using the Power Manager Wakelock (this still works but Android deprecated this functionality, so they are discouraging the use of this technique). However, as far as I can tell this is the only way I can get my application to turn the screen ON/OFF reliably.

这些都不是理想的选择(实际上,它们感觉就像是黑客一样),而只是使用更适合您的应用程序需求的那个.

None of these are ideal (in fact they feel like hacks) but just use the one that better suits your application needs.

您可以在此处阅读更多信息:

You can read more here:

  • Android Alarm Clock Source Code
  • Android Desk Clock Source Code
  • Dimming the screen to appear OFF/ON
  • Using a wakelock to keep the screen ON/OFF
  • Waking up device and turning screen ON multiple options