As far as I'm concerned. The best and actually the only practical way is to use Jni. It seems to be very confusing at first glance but if you have a little java experience, for sure you can do it.
For sending a text to another application like facebook we should use Intents. So We can simply do the job in a simple Java file and call it from c++ side using Jni. Here is the content of the SendIntent.java file. The class has a static member function that gives a context and starts the Intent. Then it sends the text data to the new activity.
package com.example.android.tools;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
public class SendIntent {
public static void sendText(Activity context,String text) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, text);
sendIntent.setType("text/plain");
context.startActivity(Intent.createChooser(sendIntent, text));
}
}
So In c++ side we just need to initiate an android activity and pass it to this class: Here is c++ code:
void example::shareText(QString str)
{
QAndroidJniEnvironment _env;
QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;"); //activity is valid
if (_env->ExceptionCheck()) {
_env->ExceptionClear();
throw InterfaceConnFailedException();
}
if ( activity.isValid() )
{
QAndroidJniObject::callStaticMethod<void>("com/example/android/tools/SendIntent","sendText","(Landroid/app/Activity;Ljava/lang/String;)V",activity.object<jobject>(),QAndroidJniObject::fromString(str).object<jstring>());
if (_env->ExceptionCheck()) {
_env->ExceptionClear();
throw InterfaceConnFailedException();
}
}else
throw InterfaceConnFailedException();
}
If you are worried about cross platform issues you can use Preprocessor directives to write platform dependent codes, this is a very common solution in c++ programming.
And the last thing that I should mention is to add these lines of code to .pro file. So qt will be able to find the java resources too:
android {
QT += androidextras
ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android-sources
}
In this case android-sources is the directory which I have placed all java sources.