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

时间:2015-12-07 10:33:33

标签: android c++ qt qandroidjniobject

我正在尝试执行此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>());

但我无法弄清楚是什么......

1 个答案:

答案 0 :(得分:0)

嵌套类名需要特殊语法:

Landroid/os/PowerManager/WakeLock

应该是:

Landroid/os/PowerManager$WakeLock

它有效!