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. How to replicate this Intent in Qt for Android?
QtWS25 Last Chance

How to replicate this Intent in Qt for Android?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
4 Posts 3 Posters 510 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.
  • R Offline
    R Offline
    Recursion
    wrote on 3 May 2023, 18:03 last edited by Recursion 5 Mar 2023, 18:16
    #1

    This intent works and does what I want on Android.

    Intent { act=android.intent.action.MAIN cat= 
    [android.intent.category.LAUNCHER] 
    cmp=SOB.Bacon/com.backend.RunBackend }
    

    I want to create an equivalent intent for Qt 6.5.0 on Android.

    What would the code for this look like? I have been looking through the Qt documentation and the sample code is not making it clear how to do this.

    This is the exact intent that ADB uses to start the service that I am trying to start on Android, so if ADB can do it from the computer then Qt should be able to do it.

    And for reference this is the ADB command that I ran which produced this intent info.

    adb shell am startforegroundservice 
    SOB.Bacon/com.backend.RunBackend
    

    I tried using just startservice but it wouldn't do it as it was in the background.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Recursion
      wrote on 3 May 2023, 19:23 last edited by
      #2

      I found an interesting link:

      I want to start an apk from my Qt under Android application

      Is this the only way to do this? Seems to me like there should be a way to do it directly from Qt.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        TomZ
        wrote on 22 May 2023, 19:23 last edited by
        #3

        @Recursion said in How to replicate this Intent in Qt for Android?:

        This intent works and does what I want on Android.

        Just quickly writing to point out that your snippet is undocumented and therefore it probably isn't actually clear to most readers what it actually does. Not even sure which language is it written in. Is that JavaScript?

        So, if you were wondering about why there were no replies, that might be a big part of it.

        1 Reply Last reply
        0
        • O Offline
          O Offline
          Odr7834
          wrote on 2 Jun 2023, 11:14 last edited by
          #4

          To create an equivalent intent for launching an application in Qt 6.5.0 on Android, you can use the QAndroidIntent class from the Qt Android Extras module. Here's an example of how you can construct the intent:

          #include <QtAndroidExtras/QAndroidJniObject>
          #include <QtAndroidExtras/QAndroidIntent>

          // ...

          QString packageName = "SOB.Bacon";
          QString className = "com.backend.RunBackend";

          QAndroidJniObject intent("android/content/Intent");
          QAndroidJniObject action = QAndroidJniObject::getStaticObjectField(
          "android/content/Intent", "ACTION_MAIN", "Ljava/lang/String;");
          QAndroidJniObject category = QAndroidJniObject::getStaticObjectField(
          "android/content/Intent", "CATEGORY_LAUNCHER", "Ljava/lang/String;");
          QAndroidJniObject componentName = QAndroidJniObject::fromString(packageName + "/" + className);

          intent.callObjectMethod("setAction", "(Ljava/lang/String;)Landroid/content/Intent;", action.object());
          intent.callObjectMethod("addCategory", "(Ljava/lang/String;)Landroid/content/Intent;", category.object());
          intent.callObjectMethod("setComponent", "(Landroid/content/ComponentName;)Landroid/content/Intent;", componentName.object());

          QAndroidJniObject startForegroundService = QAndroidJniObject::getStaticObjectField(
          "android/content/Context", "START_FOREGROUND_SERVICE", "Ljava/lang/String;");
          QtAndroid::startActivity(intent, startForegroundService);

          In the above code, we use the QAndroidJniObject class to interact with the Android Java API. We create an instance of the Intent class and set the action to ACTION_MAIN and category to CATEGORY_LAUNCHER. Then we set the component name using the package name and class name of the target application.

          Finally, we obtain the value of the START_FOREGROUND_SERVICE constant from the Context class and use it to start the activity with QtAndroid::startActivity().

          Make sure to include the necessary Qt Android Extras module in your project file (.pro file):

          QT += androidextras

          With this code, you should be able to launch the target application using an intent similar to the one you provided. If this does not help solve the problem or if I have made a mistake somewhere, then another option is to ask for help from experts in mobile development, maybe they can help you.

          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