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