FLAG_TURN_SCREEN_ON并非始终有效 [英] FLAG_TURN_SCREEN_ON does not always work

查看:658
本文介绍了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:

  • 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

这篇关于FLAG_TURN_SCREEN_ON并非始终有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆