问题
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