Controlling tablet brightness with Qt Android
I have tried the Qt forums but to no avail. I am trying to develop an android application that one of the functions changes the brightness of the tablet backlight. I have successfully written two programs in Android Studio (in Java) using the brightness of the LayoutParams system and Android putInt
to change the brightness. The problem arises when I try to move the code into a Qt application. The JNI code works for me and it does my function, but when I paste the brightness code to change the brightness method, the app doesn't work.
From what I understand from Android and error statements my problem is (I guess) I am not running code on the UI thread. I tried to get my Java method to be Runnable and use runonUiThread
, but doesn't support ContentResolver
or Window because it is not an Activity.
Does anyone have any experience with this that can help me? Or do you have any experience with Android customization?
I appreciate everyone who helps, Andrew
source to share
You can use it in a Java static 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);
}
}
Then you can call this old Java function from your C ++ code:
bool retVal = QAndroidJniObject::callStaticMethod<jint>
("com/MyApp/BrightnessChanger" // class name
, "change" // method name
, "(I)I" // signature
, 50);
This is where you pass the value from 1
and 255
to the function.
source to share