Android share button in Qt Quick

In my Qt Quick-based application whose first target platform is Android, I need to allow the user to share an image. By this I mean a general stock Android stock that is not limited to any particular social network:

enter image description here

I found some threads about sharing on FB and Twitter from Qt, but they use the Facebook / Twitter API directly, which I don't want.

From what I've found so far, it seems like there is no cross platform way to do this, and my application should probably include some Java code. If this is correct, what is the currently correct way to write code on the Android platform? How is it ?

Another idea is that it would be possible to call the share action via Javascript running on a website loaded in a WebView . Since web applications are shared this should be possible and probably more cross-platform. Does this sound reasonable?

+3


source to share


2 answers


As far as I know. The best and in fact the only practical way is to use Jni. At first glance this seems very confusing, but if you have a little java experience, you can probably do it.

To send text to another application like facebook, we have to use Intents. So we can just do the job in a simple Java file and call it from the C ++ side using Jni. Here is the content of the SendIntent.java file. The class has a static member function that gives context and starts the Intent. Then it sends 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, on the C ++ side, we just need to initiate the android activity and pass it to this class: Here is the 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 specific code, which is a very common solution in C ++ programming.

And the last thing I should mention is add these lines of code to the .pro file. This way qt will also be able to find Java resources:

android {
    QT += androidextras
ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android-sources
}

      

In this case, android sources is the directory where I put all java sources.

+6


source


I hope this post helps you too: http://blog.lasconic.com/share-on-ios-and-android-using-qml/



+2


source







All Articles