简体   繁体   English

屏幕关闭时启动活动

[英]Start activity when screen is off

I have set up an AlarmManager to start up an activity. 我已经设置了一个AlarmManager来启动一个活动。 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. 即使屏幕被锁定,如果屏幕打开也可以正常工作。

If the screen is off, it doesn't work at all. 如果屏幕关闭,则根本不起作用。 I tried using the following as the first thing in onCreate 我尝试使用以下内容作为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 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);

A while back I read that your app must be in full screen for the FLAG_TURN_SCREEN_ON to work. 前段时间我读到你的应用程序必须全屏显示才能使用FLAG_TURN_SCREEN_ON。

"** One important note. Your activity must be full screen in order for the above flag combination to work. In my app I tried to use these flags with an activity which is not full screen (Dialog Theme) and it didn't work. After looking at the documentation I found that these flags require the window to be a full screen window." “**一个重要的注意事项。你的活动必须是全屏的,以便上面的标志组合工作。在我的应用程序中,我试图使用这些标志与一个不是全屏幕的活动(对话主题),它不起作用看完文档后,我发现这些标志要求窗口是一个全屏窗口。“ - Wake Android Device up - 唤醒Android设备

Quote from someone who posted their about a similar issue with FLAG_X. 引用某人​​发布了他们与FLAG_X类似的问题。

Look into running a service, activity is going to be stopped when not in foreground. 考虑运行服务,活动将在不在前台时停止。

Also look into the Activity lifecycle. 另请参阅Activity生命周期。 http://developer.android.com/reference/android/app/Activity.html http://developer.android.com/reference/android/app/Activity.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM