PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
我想通过JNI(使用QtAndroidExtras(在C++中实现它,但我不明白(PowerManager)
的代码是做什么的。
这是类型转换吗?我想知道是否可以用QtAndroidExtras实现它,而不必编写java类。
PowerManager
是Android SDK的一个Java类。这是它的文档。
下面是如何通过 QAndroidJniObject
使用PowerManager
的示例。
爪哇代码:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
wl.acquire();
等效的Qt代码:
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" );
QAndroidJniObject wakeLock = powerMgr.callObjectMethod("newWakeLock", "(ILjava/lang/String;)Landroid/os/PowerManager$WakeLock;", levelAndFlags,tag.object<jstring>());
if ( wakeLock.isValid() )
{
wakeLock.callMethod<void>("acquire", "()V");
qDebug() << "Acquired lock!";
}
}
}
}