问题描述
我已经设置了AlarmManager来启动活动。此活动还播放声音,类似于警报应用程序或来电。
I have set up an AlarmManager to start up an activity. This activity also plays a sound, similar to an alarm app or an incoming call.
即使屏幕处于打开状态,即使屏幕已锁定,也可以正常工作。
It works ok if the screen is on, even if the screen is locked.
如果屏幕关闭,则根本不起作用。我尝试将以下内容用作onCreate中的第一件事
If the screen is off, it doesn't work at all. I tried using the following as the first thing in onCreate
getWindow().setFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON, WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
如果未启用屏幕锁,则会打开屏幕,并且我可以看到我的活动正在关闭。我听不到声音在播放。如果启用了屏幕锁,则屏幕根本不会打开。
If the screenlock is not enabled, this turns on the screen and I can see my activity closing. I can't hear the sound playing. If the screenlock is enabled, the screen won't turn on at all.
有时我会得到以下信息,但并非总是如此:
Sometimes I get the following, but not always:
07-18 23:52:13.685: E/OpenGLRenderer(14148): GL_INVALID_OPERATION
在屏幕关闭时如何使其正确启动?
How can I make it start properly when the screen is off?
推荐答案
I从部分获得了我的答案
I got my answer partially from here.
lock = ((KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE)).newKeyguardLock(KEYGUARD_SERVICE);
powerManager = ((PowerManager) getSystemService(Context.POWER_SERVICE));
wake = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
lock.disableKeyguard();
wake.acquire();
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
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
这篇关于屏幕关闭时开始活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!