Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling external activity by (explicit) intent from Qt app on Android - putExtra doesn't work

I have following problem: On Android my Qt based app (Qt 5.4.1 for mobile) calls an external activity by starting an explicit intent. This works very well in case when no data are attached to the intent. But when I use putExtra to attach a string etc the destination activity doesn't find this data.

snippet of the Qt app:

QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
if (activity.isValid())
{
    QAndroidJniObject intent("android/content/Intent", "()V");
    if (intent.isValid())
    {
        QAndroidJniObject action = QAndroidJniObject::fromString("test.app.DO_SOMETHING");
        if (action.isValid())
        {
            intent.callObjectMethod("setAction",
                                    "(Ljava/lang/String;)Landroid/content/Intent;",
                                    action.object<jobject>());

            QAndroidJniObject subject = QAndroidJniObject::fromString("test_subject");
            QAndroidJniObject text = QAndroidJniObject::fromString("test_text");
            jint flag = QAndroidJniObject::getStaticField<jint>("android/content/Intent",
                                                                "FLAG_GRANT_READ_URI_PERMISSION");

            intent.callObjectMethod("addFlags", "(I)V", flag);

            intent.callObjectMethod("putExtra",
                                    "(Ljava/lang/String;Ljava/lang/string;)Landroid/content/Intent;",
                                    subject.object<jstring>(),
                                    text.object<jstring>());

            if (intent.isValid())
                activity.callObjectMethod("startActivity", "(Landroid/content/Intent;)V", intent.object<jobject>());
        }
    }
}

snippet of the Java destination app, which tries to get the attached data:

public class Test_Activity extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        ...

        Intent intent = getIntent();

        String text = intent.getStringExtra("test_subject");//is null

        Bundle bundle = intent.getExtras();//is null

        ...
    }
}

snippet of AndroidManifest.xml (destination Java app):

<intent-filter>
    <action android:name="test.app.DO_SOMETHING" />
    <category android:name="android.intent.category.LAUNCHER" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

I tried a while to get it running but it failed. What I'm doing wrong? Could it be missing permission or flag?

Please help. Thanks a lot.

like image 922
Horst Avatar asked May 07 '15 17:05

Horst


1 Answers

May be I am very late to Answer this Question, but thought may be helpful for somebody who is looking for same functionality. This code is working perfectly fine for me, I am able to receive data using Intent.

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

QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative",
                                                                       "activity",
                                                                       "()Landroid/app/Activity;");
if (activity.isValid())
{
    QAndroidJniObject intent("android/content/Intent", "()V");
    if (intent.isValid())
    {
        QAndroidJniObject action = QAndroidJniObject::fromString("test.app.DO_SOMETHING");
        if (action.isValid())
        {
            intent.callObjectMethod("setAction",
                                    "(Ljava/lang/String;)Landroid/content/Intent;",
                                    action.object<jobject>());
            QAndroidJniObject subject = QAndroidJniObject::fromString("url");
            QAndroidJniObject text = QAndroidJniObject::fromString("http://www.google.com");


            intent.callObjectMethod("putExtra",
                                    "(Ljava/lang/String;Ljava/lang/String;)Landroid/content/Intent;",
                                    subject.object(),
                                    text.object());

            if (intent.isValid())
                activity.callMethod<void>("startActivity", "(Landroid/content/Intent;)V", intent.object<jobject>());
        }
    }
}

I made few changes in your code Using this Helpful QT Android Blog

like image 192
Swapnil Avatar answered Oct 20 '22 04:10

Swapnil