Get started when the screen is off

I created an AlarmManager to trigger an activity. This activity also plays a sound similar to an alarm application or an incoming call.

It works fine if the screen is on, even if the screen is locked.

If the screen is off, it does not work at all. I tried using the following as the first in onCreate

getWindow().setFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,  WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

If the screen lock is not turned on, this turns on the screen, and I see how the activity closes. I can not hear the sound. If the lock is on, the screen does not 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 launch it correctly when the screen is off?

+3
source share
3 answers

.

        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);
+4

, FLAG_TURN_SCREEN_ON.

"** : , . , ( ), , , , ". - Android-

-, FLAG_X.

+1

Look at the launch of the service, activity will be stopped, if not in the foreground.

Also review the Activity life cycle. http://developer.android.com/reference/android/app/Activity.html

+1
source

Source: https://habr.com/ru/post/1663787/


All Articles