Unable to call PowerManager.WakeLock.newWakeLock using QAndroidJniObject

只谈情不闲聊 提交于 2019-12-11 13:56:57

问题


I'm trying to execute this Java code:

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

using Qt QAndroidJniObject (I'm deploying a Qt app on Android).

I did that:

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

Checked from the debugger: levelAndFlags is correct (6), tag is correct ("My Tag"), but wakeLock.isValid() returns false.

There must be a problem with this line:

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

But I can't figure out what...


回答1:


Nested class names requires a special syntax:

Landroid/os/PowerManager/WakeLock

Should be:

Landroid/os/PowerManager$WakeLock

And it works!



来源:https://stackoverflow.com/questions/34131470/unable-to-call-powermanager-wakelock-newwakelock-using-qandroidjniobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!