IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

Qt Notifier

Demonstrates calling Java code from an Android application.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

Qt Notifier▲

Image non disponible

This example demonstrates how to add a custom Java class to an Android application, and how to call into this using the JNI convenience APIs in the Qt Android Extras module. The application UI is created by using Qt Quick.

Click on either of the smiley faces to put a notification in the status area of the Android device.

Running the Example▲

To run the example from http://, open the Welcome mode and select the example from Examples. For more information, visit http://.

Calling Java Methods from C++ Code▲

We define a custom Java class called NotificationClient in the NotificationClient.java file:

 
Sélectionnez
package org.qtproject.example.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;

public class NotificationClient extends org.qtproject.qt5.android.bindings.QtActivity
{
    private static NotificationManager m_notificationManager;
    private static Notification.Builder m_builder;
    private static NotificationClient m_instance;

    public NotificationClient()
    {
        m_instance = this;
    }

    public static void notify(String s)
    {
        if (m_notificationManager == null) {
            m_notificationManager = (NotificationManager)m_instance.getSystemService(Context.NOTIFICATION_SERVICE);
            m_builder = new Notification.Builder(m_instance);
            m_builder.setSmallIcon(R.drawable.icon);
            m_builder.setContentTitle("A message from Qt!");
        }

        m_builder.setContentText(s);
        m_notificationManager.notify(1, m_builder.build());
    }
}

In the NotificationClient C++ class header file, notificationclient.h, we declare a simple C++ API to display notifications on an Android device. It consists of a single string property, notification, and a slot, updateAndroidNotification(), that calls the Java code:

 
Sélectionnez
    Q_PROPERTY(QString notification READ notification WRITE setNotification NOTIFY notificationChanged)
    ...
private slots:
    void updateAndroidNotification();

A NotificationClient object is exposed to the QML in the main source file, main.cpp:

 
Sélectionnez
    QQuickView view;

    NotificationClient *notificationClient = new NotificationClient(&view);
    view.engine()->rootContext()->setContextProperty(QLatin1String("notificationClient"),
                                                     notificationClient);

In the NotificationClient C++ class source file, notificationclient.cpp, we import the QtAndroidJniObject class to be able to use its functions:

 
Sélectionnez
#include <QtAndroidExtras/QAndroidJniObject>

We connect the notificationChanged() signal to the updateAndroidNotification() slot to update the notification text when the notification property changes:

 
Sélectionnez
NotificationClient::NotificationClient(QObject *parent)
    : QObject(parent)
{
    connect(this, SIGNAL(notificationChanged()), this, SLOT(updateAndroidNotification()));
}

The NotificationClient::updateAndroidNotification() function calls the Java method. We construct a Java string from the notification string property:

 
Sélectionnez
void NotificationClient::updateAndroidNotification()
{
    QAndroidJniObject javaNotification = QAndroidJniObject::fromString(m_notification);

And pass the string object as a parameter to the Java notify() method. Note that we must supply the signature ourselves:

 
Sélectionnez
    QAndroidJniObject::callStaticMethod<void>("org/qtproject/example/notification/NotificationClient",
                                       "notify",
                                       "(Ljava/lang/String;)V",
                                       javaNotification.object<jstring>());
}

List of Files▲

Files:

List of Files▲

  • https://

  • notification/android-sources/src/org/qtproject/example/notification/NotificationClient.java

  • notification/main.cpp

  • notification/main.qrc

  • notification/notification.pro

  • notification/notificationclient.cpp

  • notification/notificationclient.h

  • notification/qml/main.qml

List of Files▲

Images:

List of Files▲
  • notification/android-sources/res/drawable/icon.png

  • notification/images/happy.png

  • notification/images/sad.png

See Also▲

See also https://, https://

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+   

Ce document est disponible sous la licence https:// telle que publiée par la Free Software Foundation.