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. Run on Android thread

Run on Android thread

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
4 Posts 3 Posters 1.3k 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.
  • V Offline
    V Offline
    vlada
    wrote on 9 Oct 2019, 08:08 last edited by
    #1

    Hi,

    I'm following this guide to implement some Android specific features in my application. The guide says that SOME Android APIs that MUST be called from Android UI thread. But how do I recognize which APIs are those? And what will happen if I call an API from Qt thread instead of Android UI thread?

    P M 2 Replies Last reply 9 Oct 2019, 17:43
    0
    • V vlada
      9 Oct 2019, 08:08

      Hi,

      I'm following this guide to implement some Android specific features in my application. The guide says that SOME Android APIs that MUST be called from Android UI thread. But how do I recognize which APIs are those? And what will happen if I call an API from Qt thread instead of Android UI thread?

      P Offline
      P Offline
      Pablo J. Rogina
      wrote on 9 Oct 2019, 17:43 last edited by
      #2

      @vlada said in Run on Android thread:

      what will happen if I call an API from Qt thread instead of Android UI thread?

      the human race will disappear from Earth...

      Seriuously, have you checked this part:

      To do such a call, from C/C++ Qt thread to Java Android UI thread, we need to do 3 steps:

      1. call a Java method from C/C++ Qt thread. The Java method will be executed in Qt thread, so we we need a way to access Android APIs in Android UI thread.

      2. our Java method uses Activity.runOnUiThread to post a runnable on Android UI thread. This runnable will be executed by the Android event loop on Android UI thread.

      3. the runnable accesses the Android APIs from Android UI thread.

      It looks like you're looking for runOnUIThread()

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      V 1 Reply Last reply 9 Oct 2019, 20:48
      0
      • V vlada
        9 Oct 2019, 08:08

        Hi,

        I'm following this guide to implement some Android specific features in my application. The guide says that SOME Android APIs that MUST be called from Android UI thread. But how do I recognize which APIs are those? And what will happen if I call an API from Qt thread instead of Android UI thread?

        M Offline
        M Offline
        mvuori
        wrote on 9 Oct 2019, 18:37 last edited by
        #3

        Basically, Android has the same principle as Qt. If an API has something to do with UI, it has to be run in the UI thread.

        What happens if you don't? You get a bug, hopefully a deterministic crash every time you execute that code; in the worst case a problem every now and then on devices that you haven't tested on.

        (I have not found any documentation about what APIs must be run in the UI thread, so it is just about following the general principle, moving execution to the UI thread and testing.)

        1 Reply Last reply
        0
        • P Pablo J. Rogina
          9 Oct 2019, 17:43

          @vlada said in Run on Android thread:

          what will happen if I call an API from Qt thread instead of Android UI thread?

          the human race will disappear from Earth...

          Seriuously, have you checked this part:

          To do such a call, from C/C++ Qt thread to Java Android UI thread, we need to do 3 steps:

          1. call a Java method from C/C++ Qt thread. The Java method will be executed in Qt thread, so we we need a way to access Android APIs in Android UI thread.

          2. our Java method uses Activity.runOnUiThread to post a runnable on Android UI thread. This runnable will be executed by the Android event loop on Android UI thread.

          3. the runnable accesses the Android APIs from Android UI thread.

          It looks like you're looking for runOnUIThread()

          V Offline
          V Offline
          vlada
          wrote on 9 Oct 2019, 20:48 last edited by vlada 10 Sept 2019, 20:51
          #4

          @Pablo-J-Rogina Yes, I know how to implement it. It is in the guide. I'm just wondering when I have to use this approach and when not.

          @mvuori Thank you. In Qt it is quite clear for me what is UI and what not. If my application isn't in foreground, I don't touch the QML part. In Android it isn't that clear.

          If you check the guide, it describes how to set SD card (un)mount listener. I would say this has nothing to do with the UI.

          I'm wondering if my approach to show foreground service is correct or not. Currently I use this method to show it:

          public class MuzikaServiceOld extends QtService
              {
                  private Handler handler;
          
                  @Override
                  public void onCreate() {
                  super.onCreate();
                  handler = new Handler();
                  }
          
                  public void showNotification(String text) {
                  runOnUiThread(new MyNotification(this, text));
                  }
          
                  private void runOnUiThread(Runnable runnable) {
                  handler.post(runnable);
                  }
          }
          

          The class MyNotification in the run() method creates and shows the notification. The question is, if this is a correct solution, because I create a new instance of the notification each time I change it.

          The second possibility is this:

          public class MuzikaServiceOld extends QtService
              {
              NotificationManager mNotificationManager;
          
                  @Override
                  public void onCreate() {
                  super.onCreate();
                  mNotificationManager = new MyNotification(this);
                  }
          
                  public void showNotification(String text) {
                  mNotificationManager.update(text);
                  }
          }
          

          This works too but in this case I'm not showing and updating the notification from Android UI thread. But I don't create a new instance on every change.

          So the question is, which of these two solutions is better and which can lead to some problems. I hope this example explains better my question.

          1 Reply Last reply
          0

          1/4

          9 Oct 2019, 08:08

          • Login

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