How to Keep the Screen on in My App

How do I keep the screen on in my App?

Use PowerManager.WakeLock class inorder to perform this.
See the following code:

import android.os.PowerManager;

public class MyActivity extends Activity {

protected PowerManager.WakeLock mWakeLock;

/** Called when the activity is first created. */
@Override
public void onCreate(final Bundle icicle) {
setContentView(R.layout.main);

/* This code together with the one in onDestroy()
* will make the screen be always on until this Activity gets destroyed. */
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
this.mWakeLock.acquire();
}

@Override
public void onDestroy() {
this.mWakeLock.release();
super.onDestroy();
}
}

Use the follwing permission in manifest file :

<uses-permission android:name="android.permission.WAKE_LOCK" />

Hope this will solve your problem...:)

How to keep screen always on android

try below code:-

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

or

import android.os.PowerManager;

public class MyActivity extends Activity {

protected PowerManager.WakeLock mWakeLock;

/** Called when the activity is first created. */
@Override
public void onCreate(final Bundle icicle) {
setContentView(R.layout.main);

/* This code together with the one in onDestroy()
* will make the screen be always on until this Activity gets destroyed. */
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
this.mWakeLock.acquire();
}

@Override
public void onDestroy() {
this.mWakeLock.release();
super.onDestroy();
}
}

for more info see below links:-

How do I keep the screen on in my App?

Android disable screen timeout while app is running

Keep the screen awake throughout my activity

As discussed in the Android tutorial Keep the Screen On, you can do this in a few ways. You can set the FLAG_KEEP_SCREEN_ON on the activity's window:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

An XML equivalent for that is to add the attribute android:keepScreenOn="true" to the root view of your activity's layout. The advantage of setting the flag programmatically is that you can use

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

when you no longer need to force the screen to stay on while your activity is running.

Another way to control the screen (and certain other resources) is to use a wake lock:

mWakeLock = ((PowerManager) getContext().getSystemService(Context.POWER_SERVICE))
.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass().getName());
mWakeLock.acquire();
// screen stays on in this section
mWakeLock.release();

The manifest will have to include this permission:

<uses-permission android:name="android.permission.WAKE_LOCK"/>

However, as discussed in the tutorial, a wake lock is more appropriate for other use cases (such as a service or background task needing the CPU to keep running while the screen is off).

how to keep app running when screen off or open another app?

You can use android.permission.WAKE_LOCK permission.

First add this permission in manifest.

<uses-permission android:name="android.permission.WAKE_LOCK" />

In your activity :

import android.os.PowerManager;

public class MyActivity extends Activity {

protected PowerManager.WakeLock mWakeLock;

@Override
public void onCreate(final Bundle icicle) {
setContentView(R.layout.main);

/* This code together with the one in onDestroy()
* will make the screen be always on until this Activity gets destroyed. */
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
this.mWakeLock.acquire();
}

@Override
public void onDestroy() {
this.mWakeLock.release();
super.onDestroy();
}
}

Or you can add this code before setContentView activity. like this:

@Override
public void onCreate(final Bundle icicle) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.main);
}

Is there a way to keep screen awake in a progressive-web-app?

Update July 2020:
Chrome 84 now ships with the Wake Lock API https://www.chromestatus.com/feature/4636879949398016 which can be used to prevent the screen from turning off.


You could use the Standby API however support is still limited.

Then there are hacks such as playing a video infinitely or the nosleep script, however from my experience they don't work consistently either.

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 can I keep my screen on using a background running app?

The response is no, you can't. As you said the only way of doing that is using deprecated api. As alternative you could use an activity with transparent theme but if the user for example press the home button and the activity is no more in foreground you loose anyway.



Related Topics



Leave a reply



Submit