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. [SOLVED] Keep Android 5 Screen On
QtWS25 Last Chance

[SOLVED] Keep Android 5 Screen On

Scheduled Pinned Locked Moved Mobile and Embedded
6 Posts 3 Posters 5.8k Views
  • 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 Offline
    L Offline
    Leonardo
    wrote on last edited by Leonardo
    #1

    Hi. I need to keep the Android device awake, because my app works processing data from the camera. I've found this code here:

    QAndroidJniObject activity = QtAndroid::androidActivity();
    if (activity.isValid()) {
        QAndroidJniObject window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;");
        if (window.isValid()) {
            const int FLAG_KEEP_SCREEN_ON = 128;
            window.callObjectMethod("addFlags", "(I)V", FLAG_KEEP_SCREEN_ON);
        }
    }
    

    It works fine on Android 4.x, and using that flag is the recommended way by this article, but on Android 5 the app crashes. The code above is the only one I could find. Does anyone know what changed on Lollipop?

    Thank you.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      Leonardo
      wrote on last edited by
      #2

      So, finally I could solve this. I'm documenting here the solution. Apparently, the crash has to do with some issue on JNI itself. I've seen some other topics unrelated to Qt with the same problem. In addition, Qt runs on another thread, so I wouldn't be able to easily call that "addFlags" method anyway. The solution was to extend QtActivity and keep the code on the Java side. Here's what I did:

      • Create a "src" folder inside "android" in the project tree

      • Create a "MyActivity.java" file inside "src"

          package com.example;
          public class MyActivity extends org.qtproject.qt5.android.bindings.QtActivity {
          	@Override
          	public void onCreate(android.os.Bundle savedInstanceState){
          		super.onCreate(savedInstanceState);
          		this.getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
          	}
          }
        
      • Edit the manifest

          <activity android:name="com.example.MyActivity" ...>
        
      D 1 Reply Last reply
      0
      • P Offline
        P Offline
        Phataas
        wrote on last edited by
        #3

        My code is identical to yours except for the code below. I have not seen any crash due to this code.

                    <Same code as you here>
        
        	//Clear any possible pending exceptions.
        	QAndroidJniEnvironment env;
        	if (env->ExceptionCheck())
        	{
        		env->ExceptionClear();
        	}
        
        1 Reply Last reply
        2
        • L Offline
          L Offline
          Leonardo
          wrote on last edited by
          #4

          Hi. Now that I understand JNI a little bit better, I've decided to give it a second chance. I'm using Qt 5.5.0 and Android 5.1.1. When I run the original code I first posted, I get:

          JNI DETECTED ERROR IN APPLICATION: the return type of CallObjectMethodV does not match void android.view.Window.addFlags(int) in call to CallObjectMethodV

          It makes sense, as I'm using callObjectMethod. So I change things a little:

          window.callMethod<void>("addFlags", "(I)V", FLAG_KEEP_SCREEN_ON);
          

          And to my surprise, it does work! I'm pretty sure I've tried it before, but I don't know why I couldn't get it to work. Maybe my lack of understanding on the topic back then was the main problem. Thank you for posting here. Now there are two solutions. Yet, extending QtActivity feels cleaner to me. I'll stick with it for now.

          1 Reply Last reply
          3
          • L Leonardo

            So, finally I could solve this. I'm documenting here the solution. Apparently, the crash has to do with some issue on JNI itself. I've seen some other topics unrelated to Qt with the same problem. In addition, Qt runs on another thread, so I wouldn't be able to easily call that "addFlags" method anyway. The solution was to extend QtActivity and keep the code on the Java side. Here's what I did:

            • Create a "src" folder inside "android" in the project tree

            • Create a "MyActivity.java" file inside "src"

                package com.example;
                public class MyActivity extends org.qtproject.qt5.android.bindings.QtActivity {
                	@Override
                	public void onCreate(android.os.Bundle savedInstanceState){
                		super.onCreate(savedInstanceState);
                		this.getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                	}
                }
              
            • Edit the manifest

                <activity android:name="com.example.MyActivity" ...>
              
            D Offline
            D Offline
            Davidisi
            wrote on last edited by
            #5

            @Leonardo Thanks for the efforts in posting your solution. I am new to QT, so this is quite difficult to get, please in what file or folder did you put the original code (JNI one), I put mine in the first line of Main.cpp, but the app crashes on the phone, please any help?
            also for the second answer, my manifest already has a <activity> tag and won't allow me add a duplicate one, is there something I am missing?
            Thanks in advance.

            L 1 Reply Last reply
            0
            • D Davidisi

              @Leonardo Thanks for the efforts in posting your solution. I am new to QT, so this is quite difficult to get, please in what file or folder did you put the original code (JNI one), I put mine in the first line of Main.cpp, but the app crashes on the phone, please any help?
              also for the second answer, my manifest already has a <activity> tag and won't allow me add a duplicate one, is there something I am missing?
              Thanks in advance.

              L Offline
              L Offline
              Leonardo
              wrote on last edited by
              #6

              You'd better extend QtActivity. You should not add a new activity, but replace the one you already have in your manifest for a custom one, following the instructions I posted above.

              <activity android:name=".MyActivity" ...>
              
              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