问题描述
我想知道是否有任何最佳实践技巧可以从 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!