Controlling Brightness of tablet with Qt Android

1.4k views Asked by At

I tried the Qt forums but with no avail. I'm trying to develop an Android application that one of the functionality is changing the brightness of the tablet backlight. I have successfully written two programs in Android Studio (in Java) using the LayoutParams and Android putInt system brightness to change brightness. The issue comes when I try to move the code to my Qt application. I have the JNI code working and it runs my functions, but when I paste the brightness code in to change brightness method the application fails.

From what I understand of Android and the error statements, my issue (I think) is I am not running the code on the UI thread. I've tried to force my Java method to be a Runnable and use runonUiThread but that doesn't support the ContentResolver or Window because it is not an Activity.

Does anyone have experience with this that can guide me? Or have any experience getting anything in the Android settings to work?

I appreciate everyone helping out, Andrew

1

There are 1 answers

0
Nejat On

You could have it in a static Java method like :

package com.MyApp;

public class BrightnessChanger
{
    public static int change(int n)
    {
        float brightness = n / (float)255;
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.screenBrightness = brightness;
        getWindow().setAttributes(lp);
    }
}

You can then call this static Java function from your C++ code :

bool retVal = QAndroidJniObject::callStaticMethod<jint>
                        ("com/MyApp/BrightnessChanger" // class name
                        , "change" // method name
                        , "(I)I" // signature
                        , 50);

Here you pass a value between 1 and 255 to the function.