C

AndroidBroadcastReceiver QML Type

Allows receiving broadcasts and filtering them with custom Intent Filters. More...

Import Statement: import QtAndroidAutomotive.Base
Since: QtAndroidAutomotive 6.5

Properties

Methods

Detailed Description

AndroidBroadcastReceiver can be used to receive broadcasts and filter then with custom Intent Filters. Here is a small example which listens to a specific action and extracts data stored in the extras property under key "data":

AndroidBroadcastReceiver {
    id: receiver
    AndroidIntentFilter {
        actions: [
            "ACTION_SEND_STORY"
        ]
        dataAuthorities: [
            {
                host: "com.qt.story"
            }
        ]
        dataPaths: [
            {
                pattern: "/story",
                matchType: AndroidMatchPattern.PATTERN_LITERAL
            }
        ]
        dataSchemes: [
            "content"
        ]
    }

    onReceived: function(intent) {
        if (intent.extras.hasOwnProperty("storytext")) {
            console.log("Received story:", intent.extras.storytext)
        }
    }
}

AndroidBroadcastReceiver lives in the UI thread, and hence will not process received broadcasts when the UI thread is in a suspended state, meaning the app is not in the foreground. Instead, new broadcasts are placed on the event queue to be processed once the UI thread resumes. If you only need to receive broadcasts while the app is in the foreground, it is recommended to call stop() when the application is about to become suspended. Broadcast receiving can be restarted when the application becomes active again via start().

Here is an example of how to follow the application state and start/stop an AndroidBroadcastReceiver when it changes:

AndroidBroadcastReceiver {
    running: Application.state === Qt.ApplicationActive

    AndroidIntentFilter {

    }
}

Property Documentation

running : bool

This property holds the status of this receiver. It will be true if this receiver is running and false if not.


Method Documentation

bool start()

Starts the receiver. Once running, it will become active and start receiving broadcasts until stopped.

Returns true if successful, false otherwise.


bool stop()

Stops the receiver. This receiver will not receive broadcasts again until started again.

Returns true if successful, false otherwise.


Available under certain Qt licenses.
Find out more.