How to lock Android device for sleep from Qt application

I am deploying a Qt application on Android and should prevent the device from going to standby (otherwise my streams are interrupted and my BLE connection is lost).

I found that on SO: How to prevent Android device programming?

This Java code must be executed:

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(); 

But how to implement this in my Qt application?

+5
source share
2 answers

QAndroidJniObject helps execute Java code from Qt. Writing this can be difficult and hard to understand what is wrong when it does not work ....

Here is the solution (wrapped 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 just do:

 { KeepAwakeHelper helper; // screen and CPU will stay awake during this section // lock will be released when helper object goes out of scope } 

Note. To use this code, you must be sure that you have the WAKE_LOCK permission set in the manifest.

+8
source

Another solution:

 QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;"); if (activity.isValid()) { QAndroidJniObject window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;"); if (window.isValid()) { const int FLAG_KEEP_SCREEN_ON = 128; window.callMethod<void>("addFlags", "(I)V", FLAG_KEEP_SCREEN_ON); } } 

Source here

I suggest you read this question for educational purposes.

+1
source

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


All Articles