简体   繁体   English

在Qt Quick中的Android上的常规共享按钮

[英]General Share button on Android in Qt Quick

In my Qt Quick-based app whose first target platform is Android I need to enable the user to share an image. 在我的Qt基于Quick的应用程序中,其第一个目标平台是Android,我需要让用户共享图像。 By that I mean the general share action on Android, which isn't limited to any particular social network: 我的意思是Android上的一般分享行动,不仅限于任何特定的社交网络:

在此输入图像描述

I did find some threads about sharing on FB and Twitter from Qt, but they use Facebook/Twitter API directly, which is not what I want. 我确实找到了一些关于在Qt上分享FB和Twitter的线索,但他们直接使用Facebook / Twitter API,这不是我想要的。

From what I found so far, it seems that there is no cross-platform way to do this and my application probably has to include some Java code. 从我到目前为止发现,似乎没有跨平台的方法来做这个,我的应用程序可能必须包含一些Java代码。 If this is correct, what is currently the proper way to write platform-specific code on Android? 如果这是正确的,那么目前在Android上编写特定于平台的代码的正确方法是什么? Like this ? 喜欢这个

Another idea is that it could be possible to invoke the share action via Javascript running in a website loaded in WebView . 另一个想法是,可以通过在WebView中加载的网站中运行的Javascript来调用共享操作。 Since web apps have sharing, this should be possible and probably more cross-platform. 由于网络应用程序具有共享功能,因此这应该是可能的,可能更具跨平台性。 Does that seem reasonable? 这看起来合情合理吗?

As far as I'm concerned. 就我而言。 The best and actually the only practical way is to use Jni. 最好的,实际上唯一可行的方法是使用Jni。 It seems to be very confusing at first glance but if you have a little java experience, for sure you can do it. 乍一看似乎很混乱,但如果你有一点java经验,肯定你可以做到。

For sending a text to another application like facebook we should use Intents. 为了将文本发送到另一个应用程序,如facebook,我们应该使用Intents。 So We can simply do the job in a simple Java file and call it from c++ side using Jni. 所以我们可以简单地在一个简单的Java文件中完成这项工作,并使用Jni从c ++端调用它。 Here is the content of the SendIntent.java file. 这是SendIntent.java文件的内容。 The class has a static member function that gives a context and starts the Intent. 该类有一个静态成员函数,它给出一个上下文并启动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: 所以在c ++方面我们只需要启动一个android活动并将它传递给这个类:这是c ++代码:

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. 如果您担心跨平台问题,可以使用预处理器指令编写与平台相关的代码,这是c ++编程中非常常见的解决方案。

And the last thing that I should mention is to add these lines of code to .pro file. 我要提到的最后一件事是将这些代码行添加到.pro文件中。 So qt will be able to find the java resources too: 所以qt也能找到java资源:

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. 在这种情况下,android-sources是我放置所有java源的目录。

我希望这篇文章也能帮到你: http//blog.lasconic.com/share-on-ios-and-android-using-qml/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM