6

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.

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?

KKO
  • 1,913
  • 3
  • 27
  • 35

3 Answers3

7

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);
Community
  • 1
  • 1
KKO
  • 1,913
  • 3
  • 27
  • 35
2

A while back I read that your app must be in full screen for the FLAG_TURN_SCREEN_ON to work.

"** 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

Quote from someone who posted their about a similar issue with FLAG_X.

Community
  • 1
  • 1
  • no luck, same behavior. the screen turns on just in time to see my activity closing – KKO Jul 18 '14 at 22:35
  • It sounded like you were having trouble with screenlock enabled. Hopefully this remedies that. If your app closes as the phone wakes up then make the run-time functionality check if the screen has woken up before it executes. Or turn to @jgriffsta's method as it would also work. – just.Blaise Jul 18 '14 at 22:42
1

Look into running a service, activity is going to be stopped when not in foreground.

Also look into the Activity lifecycle. http://developer.android.com/reference/android/app/Activity.html

johng
  • 825
  • 1
  • 9
  • 15
  • so have the activity start a service which should start the activity? would that make a difference? where would turning the screen on happen? – KKO Jul 18 '14 at 22:36
  • Mainactivity starts the service. While screen is off the service still runs in the background. The service then can display a notification/start activity etc – johng Jul 18 '14 at 22:38
  • So you meant to use a service instead of an alarm. Well, the reason why i chose an alarm is that the activity might be shown in a day, or a few days even, so there's no point in keeping a service running in a background for so long. Also, there's less chance for it to be killed by the system – KKO Jul 18 '14 at 22:44