Skip to content

Instantly share code, notes, and snippets.

@strezh
Last active May 24, 2021 17:24
Show Gist options
  • Save strezh/5201d00023b2e95f4f61 to your computer and use it in GitHub Desktop.
Save strezh/5201d00023b2e95f4f61 to your computer and use it in GitHub Desktop.
Run Android Intent from Qt (#Android #application #Qt #Launcher)
void runApplication(const QString &packageName, const QString &className)
{
qDebug() << "Start app: " <<packageName <<", "<<className;
QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod(
"org/qtproject/qt5/android/QtNative", "activity",
"()Landroid/app/Activity;"); //activity is valid
if ( activity.isValid() )
{
// Equivalent to Jave code: 'Intent intent = new Intent();'
QAndroidJniObject intent("android/content/Intent","()V");
if ( intent.isValid() )
{
QAndroidJniObject jPackageName = QAndroidJniObject::fromString(packageName);
QAndroidJniObject jClassName = QAndroidJniObject::fromString(className);
if ( jPackageName.isValid() && jClassName.isValid() )
{
// Equivalent to Jave code: 'intent.setClassName("com.android.settings", "com.android.settings.DevelopmentSettings");'
intent.callObjectMethod("setClassName",
"(Ljava/lang/String;Ljava/lang/String;)Landroid/content/Intent;",
jPackageName.object<jstring>(),jClassName.object<jstring>());
jint flag = QAndroidJniObject::getStaticField<jint>(
"android/content/Intent",
"FLAG_ACTIVITY_NEW_TASK");
intent.callObjectMethod("setFlags", "(I)V",flag);
// Equivalent to Jave code: 'startActivity(intent);'
QAndroidJniEnvironment env;
activity.callObjectMethod(
"startActivity",
"(Landroid/content/Intent;)V",
intent.object<jobject>());
if (env->ExceptionCheck()) {
qDebug() << "Intent not found!";
env->ExceptionClear(); // TODO: stupid method! Remove this!
}
} else {
qDebug() << "Action is not valid";
}
} else {
qDebug() << "Intent is not valid";
}
} else {
qDebug() << "Activity is not valid";
}
}
@feichtinger
Copy link

Hi, could you please upload an example project or similar? How can I use this function?

Thanks a lot.

@forlayo
Copy link

forlayo commented Mar 20, 2017

For those that are getting
JNI DETECTED ERROR IN APPLICATION: the return type of CallObjectMethodV does not match void android.app.Activity.startActivity(android.content.Intent)

You should use
activity.callMethod<void>("startActivity","(Landroid/content/Intent;)V", intent.object<jobject>());

Instead of
activity.callObjectMethod("startActivity","(Landroid/content/Intent;)V",intent.object<jobject>());

Starting from Android 5.x as filled in this QTBUG -> https://bugreports.qt.io/browse/QTBUG-48631

@kuku83
Copy link

kuku83 commented Apr 24, 2019

Hi, i am trying to Intent a Qt app from another Qt app (qt+android both) and always recieving "intent not found" :-(

What can i do?

Here a part of the manifest

    <activity android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|layoutDirection|locale|fontScale|keyboard|keyboardHidden|navigation|mcc|mnc|density" android:name="org.qtproject.qt5.android.bindings.QtActivity" android:label="Devolucion" android:screenOrientation="landscape" android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment