Qt/QML Android 发送自定义 Intent 的最佳实践(分享 URL)

Qt/QML Android best practice to send a custom Intent (share URL)(Qt/QML Android 发送自定义 Intent 的最佳实践(分享 URL))
本文介绍了Qt/QML Android 发送自定义 Intent 的最佳实践(分享 URL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有任何最佳实践技巧可以从 QML(或 c++)发送自定义 android 意图.

I was wondering if there are any best practice tips for sending custom android intents from QML (or c++ for that matter).

我应该创建一个自定义的 android 活动并使用 QAndroidJniObject 类来调用它还是有更好的方法?

should I create a custom android activity and use the QAndroidJniObject class to call it or are there any better ways?

我的目的是创建一个从 QML 到其他 Android 应用程序的简单共享 URL 功能.

My intention is to create a simple share URL function from QML to other android apps.

谢谢

推荐答案

用额外的静态方法扩展QtActivity:

Extend QtActivity with additional static method:

package org.whatever

public class YourActivity extends org.qtproject.qt5.android.bindings.QtActivity
{
    private static YourActivity instance;

    YourActivity() {
        instance = this;
    }

    public static void shareUrl(QString url) {
        //create intent here
        //can use instance object
    }
}

在c++端使用QAndroidJniObject调用shareUrl方法

On c++ side call shareUrl method using QAndroidJniObject

class QmlInterface : public QObject
{
    Q_OBJECT
    public:
        QmlInterface();
        Q_INVOKABLE void shareUrl( QString url );
};

和实施:

void QmlInterface:: shareUrl( QString url )
{
#ifdef Q_OS_ANDROID
    QAndroidJniObject::callStaticMethod( "org/whatever/YourActivity",
                                         "shareUrl",
                                         "(Ljava/lang/String;)V",
                                         QAndroidJniObject::fromString( url ));
#endif
}

在 java 端使用静态方法可以显着简化 jni 调用,因为您不必获取 Activity 实例.因为需要Activity上下文来发送Intent静态instance成员对象用在java端.

Using static method on java side simplifies jni call significantly because you don't have to get Activity instance. Because Activity context is needed to send Intent static instance member object is used on java side.

这篇关于Qt/QML Android 发送自定义 Intent 的最佳实践(分享 URL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)