How do I prevent an Android device from going to sleep programmatically?
One option is to use a wake lock. Example from the docs:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
wl.acquire();
// screen and CPU will stay awake during this section
wl.release();
There's also a table on this page that describes the different kinds of wakelocks.
Be aware that some caution needs to be taken when using wake locks. Ensure that you always release()
the lock when you're done with it (or not in the foreground). Otherwise your app can potentially cause some serious battery drain and CPU usage.
The documentation also contains a useful page that describes different approaches to keeping a device awake, and when you might choose to use one. If "prevent device from going to sleep" only refers to the screen (and not keeping the CPU active) then a wake lock is probably more than you need.
You also need to be sure you have the WAKE_LOCK permission set in your manifest in order to use this method.
Disabling screen sleep Programmatically in Android?
setKeepScreenOn(true)
Disable sleep on certain activity
Try this.
@Override
protected void onCreate(Bundle icicle)
{
super.onCreate(icicle);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
How do I prevent an Android device from going to sleep from Qt application
QAndroidJniObject helps executing Java code from Qt. Writting it could be hard and it's hard to figure out what's wrong when it does not work....
Here is the solution (encapsulated in a helper class) to lock a PowerManager.WakeLock
object:
LockHelper.h:
#pragma once
#include <QAndroidJniObject>
class KeepAwakeHelper
{
public:
KeepAwakeHelper();
virtual ~KeepAwakeHelper();
private:
QAndroidJniObject m_wakeLock;
};
LockHelper.cpp:
#include "LockHelper.h"
#include <QAndroidJniObject>
#include <QDebug>
#include "jni.h"
KeepAwakeHelper::KeepAwakeHelper()
{
QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
if ( activity.isValid() )
{
QAndroidJniObject serviceName = QAndroidJniObject::getStaticObjectField<jstring>("android/content/Context","POWER_SERVICE");
if ( serviceName.isValid() )
{
QAndroidJniObject powerMgr = activity.callObjectMethod("getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;",serviceName.object<jobject>());
if ( powerMgr.isValid() )
{
jint levelAndFlags = QAndroidJniObject::getStaticField<jint>("android/os/PowerManager","SCREEN_DIM_WAKE_LOCK");
QAndroidJniObject tag = QAndroidJniObject::fromString( "My Tag" );
m_wakeLock = powerMgr.callObjectMethod("newWakeLock", "(ILjava/lang/String;)Landroid/os/PowerManager$WakeLock;", levelAndFlags,tag.object<jstring>());
}
}
}
if ( m_wakeLock.isValid() )
{
m_wakeLock.callMethod<void>("acquire", "()V");
qDebug() << "Locked device, can't go to standby anymore";
}
else
{
assert( false );
}
}
KeepAwakeHelper::~KeepAwakeHelper()
{
if ( m_wakeLock.isValid() )
{
m_wakeLock.callMethod<void>("release", "()V");
qDebug() << "Unlocked device, can now go to standby";
}
}
Then, simply do:
{
KeepAwakeHelper helper;
// screen and CPU will stay awake during this section
// lock will be released when helper object goes out of scope
}
Note: You need to be sure you have the WAKE_LOCK permission set in your manifest in order to use this code.
prevent mobile from going into sleep mode when app is running
You need a WakeLock. There are different kinds of wake locks so be sure to select the least aggressive one that meets your needs. In particular it sounds like you need a Partial Wake Lock.
Partial Wake Lock - Wake lock that ensures that the CPU is running. The screen might not be on.
Also, make sure you add the permission android.permission.WAKE_LOCK
to your manifest. And finally be double sure to Release your lock when you are done.
Keep Android screen from going to sleep on specific screen (LibGDX)
You can use Interfacing for the same.
Create an interface inside your core
module
public interface Service {
void keepScreenOn(boolean isOn);
}
Implement this interface in android module
public class AndroidLauncher extends AndroidApplication implements Service {
View gameView;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gameView=initializeForView(new Main(this), config);
setContentView(gameView);
}
@Override
public void keepScreenOn(final boolean isOn) {
runOnUiThread(new Runnable() {
@Override
public void run() {
gameView.setKeepScreenOn(isOn);
}
});
}
}
Main/ApplicationListener class inside core module
public class Main extends ApplicationAdapter {
Service service;
public Main(Service service){
this.service=service;
}
}
You've reference of Service
, When you move to particular screen then can call keepScreenOn()
of Service interface.
I've not tested this but it should work.
Related Topics
How to Set Onclicklistener on a Radiobutton in Android
How to Hide a Menu Item in the Actionbar
Replace Selector Images Programmatically
How to Ignore Accent in SQLite Query (Android)
How to Display Inline Images from HTML in an Android Textview
Zxing Camera in Portrait Mode on Android
Difference Between .Getpath() VS Cursor in Getting the Real Path of a File from Uri in Android
How to Detect If I am in Release or Debug Mode
How to Zip and Unzip a String Using Gzipoutputstream That Is Compatible with .Net
How to Get Return Value from JavaScript in Webview of Android
Media Player Called in State 0, Error (-38,0)
Formula Px to Dp, Dp to Px Android
How to Change the Radio Button Icon in an Android Radio Button Group
Remove Data from Notification Intent
How to Set a Reminder in Android