Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Backlight control for Android 4.2 in Qt project
Forum Updated to NodeBB v4.3 + New Features

Backlight control for Android 4.2 in Qt project

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
11 Posts 3 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L LeBol

    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.

    raven-worxR Offline
    raven-worxR Offline
    raven-worx
    Moderators
    wrote on last edited by raven-worx
    #2

    @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.

    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
    If you have a question please use the forum so others can benefit from the solution in the future

    L KroMignonK 3 Replies Last reply
    3
    • raven-worxR raven-worx

      @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.

      L Offline
      L Offline
      LeBol
      wrote on last edited by
      #3

      Thank 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

      1 Reply Last reply
      0
      • raven-worxR raven-worx

        @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.

        L Offline
        L Offline
        LeBol
        wrote on last edited by
        #4

        @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-worxR 1 Reply Last reply
        0
        • L LeBol

          @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-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #5

          @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?

          https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#screenBrightness

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          L 1 Reply Last reply
          0
          • raven-worxR raven-worx

            @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.

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by
            #6

            @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!

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            raven-worxR 1 Reply Last reply
            0
            • KroMignonK KroMignon

              @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-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #7

              @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.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • raven-worxR raven-worx

                @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?

                https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#screenBrightness

                L Offline
                L Offline
                LeBol
                wrote on last edited by
                #8

                @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-worxR 1 Reply Last reply
                0
                • L LeBol

                  @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-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #9

                  @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()

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  L 1 Reply Last reply
                  0
                  • raven-worxR raven-worx

                    @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()

                    L Offline
                    L Offline
                    LeBol
                    wrote on last edited by
                    #10

                    @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-worxR 1 Reply Last reply
                    0
                    • L LeBol

                      @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-worxR Offline
                      raven-worxR Offline
                      raven-worx
                      Moderators
                      wrote on last edited by raven-worx
                      #11

                      @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)

                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                      If you have a question please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved