Calling external activity using (explicit) intent from a Qt application on Android - putExtra not working

I have the following problem: On Android, my Qt-based application (Qt 5.4.1 for mobile) invokes an external action by running an explicit intent. This works very well if no data is bound to the intent. But when I use putExtra to append a string, etc., Destination activity does not find this data.

Qt application fragment:

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>()); } } } 

A fragment of a Java application that is trying to get 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 ... } } 

fragment of AndroidManifest.xml file (target Java application):

 <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 for a while to launch it, but it failed. What am I doing wrong? Could a permission or flag be missing?

Please, help. Thank you very much.

+6
source share
2 answers

Maybe I'm very late to answer this question, but the thought may be useful for those who are looking for the same functionality. This code works fine for me, I can get 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 a few changes to your code. Using this useful QT Android Blog

0
source

You need to create a class class Picker: public QAndroidActivityResultReceiver with the void handleActivityResult function (int receiverRequestCode, int resultCode, const QAndroidJniObject & data) and start with const int REQUEST_CODE = 42; QtAndroid :: startActivity (intent, REQUEST_CODE, collector);

So you can use my sample in github repo

https://github.com/tripolskypetr/qmlchooseimage

-1
source

Source: https://habr.com/ru/post/986769/


All Articles