gpt4 book ai didi

android - 无法使用 QAndroidJniObject 调用 PowerManager.WakeLock.newWakeLock

转载 作者:太空宇宙 更新时间:2023-11-04 13:23:33 30 4
gpt4 key购买 nike

我正在尝试执行这段 Java 代码:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
wl.acquire();

使用 Qt QAndroidJniObject(我正在 Android 上部署 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");
else
assert( false );
}
}
}

从调试器检查:levelAndFlags 是正确的 (6),tag 是正确的(“我的标签”),但是 wakeLock.isValid() 返回 false。

这行肯定有问题:

QAndroidJniObject wakeLock = powerMgr.callObjectMethod("newWakeLock", "(ILjava/lang/String;)Landroid/os/PowerManager/WakeLock;", levelAndFlags,tag.object<jstring>());

但是我想不通是什么...

最佳答案

嵌套类名需要特殊语法:

Landroid/os/PowerManager/WakeLock

应该是:

Landroid/os/PowerManager$WakeLock

而且有效!

关于android - 无法使用 QAndroidJniObject 调用 PowerManager.WakeLock.newWakeLock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34131470/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com