Keep active when lock screen when alseep screens

So, I created an application that functions as a lock screen replacement. I use the broadcast receiver and service to start my activity after Intent.ACTION_SCREEN_OFF . So that every time the user locks the screen, my activity starts, and then when they press the unlock button, my activity is already running on the lock screen. But this only works if the user tries to wake / unlock the phone after a short period of time. If they wait too long, activity will disappear. I’m not sure why this is happening and what I can do to stay active there, no matter how long the user waits to unlock their phone.

I thought and tried to listen to Intent.ACTION_SCREEN_ON , but then between the time when the user presses the power button on his phone, he wakes him up and when the application loads and appears on the screen. During this gap, the user can see the Android OS.

+7
source share
2 answers

What if you use wakelock . For example:

 @Override public void onCreate(Bundle savedInstanceState) { PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag"); wl.acquire(); // do your things, even when screen is off } @Override protected void onDestroy() { wl.release(); } 

You must also have wakelock permission AndroidManifest.xml

 uses-permission android:name="android.permission.WAKE_LOCK" 
+3
source

One way you can try is to make sure the application never sleeps. With short dreams, it will work. With a long sleep, your application sleeps. I was able to get around this myself using PowerManager.Wakelock. Only problem is that it will drain more battery if your application uses CPU cycles.

 /** wake lock on the app so it continues to run in background if phone tries to sleep.*/ PowerManager.WakeLock wakeLock; @Override public void onCreate(Bundle savedInstanceState) { ... // keep the program running even if phone screen and keyboard go to sleep PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); ... } // use this when screen sleeps wakeLock.acquire(); // use this once when phone stops sleeping wakeLock.release(); 
+1
source

All Articles