Important: Please read the Qt Code of Conduct -

Android GetSystemService for Power Manager via Qt

  • Hi All,

    I need some help in using the power services manager in Android. THe code that I need to write in Java is :
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

    To do this in Qt I have done :
    @QAndroidJniObject activity = QtAndroid::androidActivity()
    QAndroidJniObject context = activity.callObjectMethod("getApplicationContext","()Landroid/content/Context;");
    qDebug() << "Context Not Valid";
    QAndroidJniObject powerString = QAndroidJniObject::fromString("power");
    powerManager = context.callObjectMethod("getSystemService", "(Ljava/lang/String;)Landroid/os/PowerManager;", powerString.object<jstring>());
    qDebug() << "Power manager invalid";
    powerManager.callObjectMethod( "isScreenOn","()Z");

    I keep getting that power manager is invalid.
    What am I doing wrong?

    I have also tried with @QAndroidJniObject powerString = QAndroidJniObject::fromString("POWER_SERVICE");@ ,
    but still the powerManager is invalid! :(

    Can someone please give me a clue as to how to fix this, or a work around...

    Thanks so much!

  • getSystemService(String) returns an Java Object so try

    powerManager = context.callObjectMethod("getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;", powerString.object<jstring>());

Log in to reply