C
QAndroidBroadcastTransmitter Class
Utility class for sending broadcasts. (Technical Preview). More...
Header: | #include <QAndroidBroadcastTransmitter> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS AndroidAutomotiveBase) target_link_libraries(mytarget PRIVATE Qt6::AndroidAutomotiveBase) |
Since: | QtAndroidAutomotive 6.5 |
Inherits: | QObject |
Public Types
Public Functions
QAndroidBroadcastTransmitter(QObject *parent = nullptr) | |
virtual | ~QAndroidBroadcastTransmitter() override |
void | sendBroadcast(const QAndroidIntentInfo &intent, const QString &receiverPermission = QString()) |
void | setBundleFunctor(QAndroidBroadcastTransmitter::BundleFunctor functor) |
Static Public Members
void | setGlobalBundleFunctor(QAndroidBroadcastTransmitter::BundleFunctor functor) |
Detailed Description
This class enables the user to send broadcasts by using sendBroadcast.
Here is a basic example of a QAndroidIntentInfo sent via QAndroidBroadcastTransmitter:
QAndroidIntentInfo intent; intent.setAction("org.qt.io.example.action.ACTION_DATE"); QVariantMap extras = { { "date", QDate::currentDate() } }; intent.setExtras(extras); QAndroidBroadcastTransmitter transmitter; transmitter.sendBroadcast(intent);
The intent class used to send a broadcast will have the QVariant extras converted to their Java equivalents via the use of internal and external conversion functions, called BundleFunctor. This API offers an internal set of default conversion functions, which handle the following conversions:
C++ type | Java type |
---|---|
QString | String |
int | int |
short | short |
unsigned short | char |
char unsigned char | byte |
long long | long |
double | double |
bool | boolean |
QVariantMap | Bundle, generated by recursive calls to this conversion function. |
QByteArray | byte[] |
QVariantList | The following types are supported. QVariantLists that have more than one type of variable are discarded.
|
If these default converters do not suffice or the user wants to send some custom types, the API offers local and global BundleFunctor converter functions that can be set via setBundleFunctor and setGlobalBundleFunctor.
These BundleFunctors will be used when packaging up the broadcast extras data for the broadcast. The local BundleFunctor will only be used by the instance of QAndroidBroadcastTransmitter it has been registered to, whereas global BundleFunctors will be used by all instances of QAndroidBroadcastTransmitter.
Member Type Documentation
QAndroidBroadcastTransmitter::BundleFunctor
This is a typedef for a special type of function used to convert data from a QVariant representation to a Bundle referred to by a QJniObject.
The function should return true
when it has finished converting the given value, or false
if the function should or could not be used to convert the value. This means that if the user does not want the given value to be converted at all, they may return false
without adding any data to the given QJniObject.
The first argument will be a reference to a QJniObject representation of an Android Bundle.
The second argument will be the QVariant data that should be converted. This data may be the entire QVariantMap passed from sendBroadcast() or a single QVariant from that QVariantMap.
Users may detect whether the argument is a QVariant or a QVariantMap by using the QVariant::metaType() method and the qMetaTypeId macro like so:
QVariant variant = QVariantMap(); if (variant.metaType().id() == qMetaTypeId<QVariantMap>()) { // QVariant variable contains a QVariantMap. }
The third argument will be the path of keys from the root of the QVariantMap to the current value, or empty if the passed QVariant data is the full QVariantMap data passed to sendBroadcast().
This code snippet shows a BundleFunctor being used to write the year, month and day from a QDate contained inside a QVariant into an Android Bundle which is then written into the bundle provided by QAndroidBroadcastTransmitter. This way we can contain all the required date data inside a single Bundle, identified via the key "date".
Note that when we're done processing the QDate, we return true
in order to make sure that the default converter functions are not used on this data.
Q_DECLARE_JNI_CLASS(AndroidBundle, "android/os/Bundle") const QAndroidBroadcastTransmitter::BundleFunctor dateConverter = [] ( QJniObject &bundle, const QVariant &variant, const QStringList &keys) { if (keys.empty() || keys.last() != "date") return false; if (variant.metaType().id() != qMetaTypeId<QDate>()) return false; QJniObject dateBundle(QtJniTypes::className<QtJniTypes::AndroidBundle>()); QDate date = variant.toDate(); const auto yearKey = QJniObject::fromString("year"); const auto yearString = yearKey.object<jstring>(); dateBundle.callMethod<void>("putInt", yearString, date.year()); const auto monthKey = QJniObject::fromString("month"); const auto monthString = monthKey.object<jstring>(); dateBundle.callMethod<void>("putInt", monthString, date.month()); const auto dayKey = QJniObject::fromString("day"); const auto dayString = dayKey.object<jstring>(); dateBundle.callMethod<void>("putInt", dayString, date.day()); const auto dateKey = QJniObject::fromString("date"); const auto dateString = dateKey.object<jstring>(); bundle.callMethod<void>("putBundle", dateString, dateBundle.object<QtJniTypes::AndroidBundle>()); return true; };
See also setBundleFunctor().
Member Function Documentation
[explicit]
QAndroidBroadcastTransmitter::QAndroidBroadcastTransmitter(QObject *parent = nullptr)
Constructs a QAndroidBroadcastTransmitter with the given parent.
[override virtual noexcept]
QAndroidBroadcastTransmitter::~QAndroidBroadcastTransmitter()
Destroys the QAndroidBroadcastTransmitter.
void QAndroidBroadcastTransmitter::sendBroadcast(const QAndroidIntentInfo &intent, const QString &receiverPermission = QString())
Sends a broadcast with the given intent. Caller may optionally require the receiver to have permission receiverPermission to receive this broadcast.
void QAndroidBroadcastTransmitter::setBundleFunctor(QAndroidBroadcastTransmitter::BundleFunctor functor)
Sets the local QAndroidBroadcastTransmitter::BundleFunctor to functor.
[static]
void QAndroidBroadcastTransmitter::setGlobalBundleFunctor(QAndroidBroadcastTransmitter::BundleFunctor functor)
Sets the global QAndroidBroadcastTransmitter::BundleFunctor to functor.
Note: Global BundleFunctors will be used by all instances of QAndroidBroadcastTransmitter.
Available under certain Qt licenses.
Find out more.