Keep Screen Alive and App in Front of Lockscreen

How to keep application awake in flutter?

As support for the screen plugin that @Tree mentioned has been discontinued and there are some issues with it now, you can use wakelock.

Full disclosure: I am the author of this plugin, however, it is basically a port of the wakelock functionality from the screen plugin, with the issues fixed:

import 'package:wakelock/wakelock.dart';

// To keep the screen on:
Wakelock.enable(); // or Wakelock.toggle(on: true);

// To let the screen turn off again:
Wakelock.disable(); // or Wakelock.toggle(on: false);

Learn more.

How wake up device and show an activity on top of lock screen for alarm?

Just have to set the correct window flags. This works from API 14(?) up to Oreo Beta:


[Activity(Label = "AlarmActivity")]
public class AlarmActivity : Activity, View.IOnClickListener
protected override void OnCreate(Bundle savedInstanceState)

public override void OnAttachedToWindow()
Window.AddFlags(WindowManagerFlags.ShowWhenLocked |
WindowManagerFlags.KeepScreenOn |
WindowManagerFlags.DismissKeyguard |

public void OnClick(View v)

AlarmManager Test:

Call this routine and the alarm manager will start up the AlarmActivity in one minute, so apply this within a Button click/listener and lock your screen:

using (var manager = (Android.App.AlarmManager)GetSystemService(AlarmService))
using (var calendar = Calendar.Instance)
calendar.Add(CalendarField.Minute, 1);
Log.Debug("SO", $"Current date is : {Calendar.Instance.Time.ToString()}");
Log.Debug("SO", $"Alarm will fire at {calendar.Time.ToString()}");
var alarmIntent = new Intent(ApplicationContext, typeof(AlarmActivity));
var pendingIntent = PendingIntent.GetActivity(this, 0, alarmIntent, PendingIntentFlags.OneShot);
manager.SetExact(AlarmType.RtcWakeup, calendar.TimeInMillis, pendingIntent);

Starting activity from service on lock screen turns on the screen but does not show the activity itself

I'm facing the same problem, after a lot of searching here and google, found this which unlocked the screen and popped my activity but it only works for me when the app is running (foreground/background).

import android.view.Window;
import android.view.WindowManager.LayoutParams;

Window window = this.getWindow();

i'm trying to start an activty when app is closed... (using broadcast receiver)

in the docs (for example here) and most of the answers on SO the flags are added this way:


but when i tried the way it is like in the example it unlocked the screen instead of just turning on the screen.

hope this help . it still didn't solve my problem completely.


found this post which solved my problem.

there is a comment there on NOT using a dialog theme which solved it for me

Preventing Android app from closing

Something like this with enhancements? Source of Android's lock screen or How to lock the Screen customly? Just like WaveSecure in Android

Maybe take a look at this source code and use it to extend the functionality of your app to lock to just your program.

The WaveSecure App requires entering a password before it allows access to the phone.

Edit: might give you the options you want too.

Lock screen compatible with modals

Make your lock screen a window and position it in front of all modal system windows, see the View Programming Guidelines for details.


lockWindow.windowlevel = UIWindowLevelStatusBar + 0.05f:

Is there a Jetpack Compose equivalent for android:keepScreenOn to keep screen alive?

You can use LocalContext to get activity, and it has a window on which you can apply needed flags.

In such cases, when you need to run some code on both view appearance and disappearance, DisposableEffect can be used:

fun KeepScreenOn() {
val context = LocalContext.current
DisposableEffect(Unit) {
val window = context.findActivity()?.window
onDispose {

fun Context.findActivity(): Activity? {
var context = this
while (context is ContextWrapper) {
if (context is Activity) return context
context = context.baseContext
return null

Usage: when screen appears flag is set to on, and when disappears - it's cleared.

fun Screen() {

As @Louis CAD correctly pointed out, you can have problems if you use this "view" in many views: if one view appears that uses it, and then disappears previous views that also used it, it will reset the flag.

I haven't found a way of tracking flags state to update the view, I think @Louis CAD solution is OK until Compose have some system support.

Related Topics

Leave a reply