Backlight control for Android 4.2 in Qt project
-
wrote on 27 Mar 2019, 15:37 last edited by
Hi.
We are porting our Qt application to Android platform. We made it basically working. However the remaining problem is a backlight control. Can anyone suggest a solution for such function. -
Hi.
We are porting our Qt application to Android platform. We made it basically working. However the remaining problem is a backlight control. Can anyone suggest a solution for such function.@LeBol
not without using JNI and calling Android API.
You could subclass Qt's android activity and implement a method doing some JAVA stuff, like:private void setBrightness(int brightness) // brightness [0..100] { Window w = getWindow(); WindowManager.LayoutParams lp = w.getAttributes(); lp.screenBrightness = (float)brightness/100.0; if (lp.screenBrightness<.01f) lp.screenBrightness=.01f; w.setAttributes(lp); }
Then call this method via
jint brightness = 75; QtAndroid::activity()->callMethod<void>("setBrightness", "(I)V", brightness);
Or the same JAVA code completely in C++ using QAndroidJniObjects (a bit more cumbersome).
Note: i haven't tested the JAVA code though.
-
@LeBol
not without using JNI and calling Android API.
You could subclass Qt's android activity and implement a method doing some JAVA stuff, like:private void setBrightness(int brightness) // brightness [0..100] { Window w = getWindow(); WindowManager.LayoutParams lp = w.getAttributes(); lp.screenBrightness = (float)brightness/100.0; if (lp.screenBrightness<.01f) lp.screenBrightness=.01f; w.setAttributes(lp); }
Then call this method via
jint brightness = 75; QtAndroid::activity()->callMethod<void>("setBrightness", "(I)V", brightness);
Or the same JAVA code completely in C++ using QAndroidJniObjects (a bit more cumbersome).
Note: i haven't tested the JAVA code though.
wrote on 28 Mar 2019, 13:06 last edited byThank you very much. We will try.
However, is there misspell here?
@raven-worx said in Backlight control for Android 4.2 in Qt project:QtAndroid::activity()->callMethod<void>("setBrightness", "(I)V", brightness);
LeBol
-
@LeBol
not without using JNI and calling Android API.
You could subclass Qt's android activity and implement a method doing some JAVA stuff, like:private void setBrightness(int brightness) // brightness [0..100] { Window w = getWindow(); WindowManager.LayoutParams lp = w.getAttributes(); lp.screenBrightness = (float)brightness/100.0; if (lp.screenBrightness<.01f) lp.screenBrightness=.01f; w.setAttributes(lp); }
Then call this method via
jint brightness = 75; QtAndroid::activity()->callMethod<void>("setBrightness", "(I)V", brightness);
Or the same JAVA code completely in C++ using QAndroidJniObjects (a bit more cumbersome).
Note: i haven't tested the JAVA code though.
wrote on 28 Mar 2019, 13:20 last edited by@raven-worx I am somewhat confused why the backlight value is converted to the range 0.01-1.0?
Is it not 0 -255? -
@raven-worx I am somewhat confused why the backlight value is converted to the range 0.01-1.0?
Is it not 0 -255?@LeBol said in Backlight control for Android 4.2 in Qt project:
However, is there misspell here?
what exactly do you mean?
Is it not 0 -255?
-
@LeBol
not without using JNI and calling Android API.
You could subclass Qt's android activity and implement a method doing some JAVA stuff, like:private void setBrightness(int brightness) // brightness [0..100] { Window w = getWindow(); WindowManager.LayoutParams lp = w.getAttributes(); lp.screenBrightness = (float)brightness/100.0; if (lp.screenBrightness<.01f) lp.screenBrightness=.01f; w.setAttributes(lp); }
Then call this method via
jint brightness = 75; QtAndroid::activity()->callMethod<void>("setBrightness", "(I)V", brightness);
Or the same JAVA code completely in C++ using QAndroidJniObjects (a bit more cumbersome).
Note: i haven't tested the JAVA code though.
wrote on 28 Mar 2019, 14:27 last edited by@raven-worx said in Backlight control for Android 4.2 in Qt project:
private void setBrightness(int brightness) // brightness [0..100]
Looks good for me, but I think setBrightness() should be public not private!
-
@raven-worx said in Backlight control for Android 4.2 in Qt project:
private void setBrightness(int brightness) // brightness [0..100]
Looks good for me, but I think setBrightness() should be public not private!
@KroMignon said in Backlight control for Android 4.2 in Qt project:
Looks good for me, but I think setBrightness() should be public not private!
JNI doesn't care about visibility.
-
@LeBol said in Backlight control for Android 4.2 in Qt project:
However, is there misspell here?
what exactly do you mean?
Is it not 0 -255?
wrote on 28 Mar 2019, 18:02 last edited by@raven-worx
Thanks for the link regarding the backlight value range. I am total idiot with respect to Android API's.
'
However. What I meant by misspell.
I refer to your code you suggested earlier
QtAndroid::activity()->callMethod<void>("setBrightness", "(I)V", brightness);
Is this correct? There is no function activity() in QtAndroid namespace. -
@raven-worx
Thanks for the link regarding the backlight value range. I am total idiot with respect to Android API's.
'
However. What I meant by misspell.
I refer to your code you suggested earlier
QtAndroid::activity()->callMethod<void>("setBrightness", "(I)V", brightness);
Is this correct? There is no function activity() in QtAndroid namespace.@LeBol said in Backlight control for Android 4.2 in Qt project:
Is this correct? There is no function activity() in QtAndroid namespace.
right. QtAndroid::androidActivity()
-
@LeBol said in Backlight control for Android 4.2 in Qt project:
Is this correct? There is no function activity() in QtAndroid namespace.
right. QtAndroid::androidActivity()
wrote on 29 Mar 2019, 08:49 last edited by@raven-worx
You've suggested to subclass AndroidActivity - in order to override setBrightness.Don't we need to create a dynamic object (of this subclass) rather than calling the method statically?
-
@raven-worx
You've suggested to subclass AndroidActivity - in order to override setBrightness.Don't we need to create a dynamic object (of this subclass) rather than calling the method statically?
@LeBol
in order for your code to be executed there must be already an activity up and running (the current instance is returned by QtAndroid::androidActivity()).
You need to specify your custom Activity class in the AndroidManifest.xml
No static methods involved (on the JAVA side)
1/11