Qt Android Notifier
Android アプリケーションで Qt から Java コードを呼び出す例を示します。
この例では、Android アプリケーションにカスタム Java クラスを追加し、Qt の JNI 便利 API を使用して呼び出す方法を示します。
スマイリーフェイスをクリックすると、Android画面のステータスバーに通知が表示されます。
サンプルを実行する
からサンプルを実行するには Qt Creatorからサンプルを実行するには、Welcome モードを開き、Examples からサンプルを選択します。詳細については、Building and Running an Exampleを参照してください。
C++コードからJavaメソッドを呼び出す
NotificationClient.javaファイルにNotificationClient
というカスタムJavaクラスを定義します:
package org.qtproject.example.androidnotifier; import android.app.Notification; import android.app.NotificationManager; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Color; import android.graphics.BitmapFactory; import android.app.NotificationChannel; public class NotificationClient { public static void notify(Context context, String message) { try { NotificationManager m_notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Notification.Builder m_builder; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel notificationChannel; notificationChannel = new NotificationChannel("Qt", "Qt Notifier", importance); m_notificationManager.createNotificationChannel(notificationChannel); m_builder = new Notification.Builder(context, notificationChannel.getId()); } else { m_builder = new Notification.Builder(context); } Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon); m_builder.setSmallIcon(R.drawable.icon) .setLargeIcon(icon) .setContentTitle("A message from Qt!") .setContentText(message) .setDefaults(Notification.DEFAULT_SOUND) .setColor(Color.GREEN) .setAutoCancel(true); m_notificationManager.notify(0, m_builder.build()); } catch (Exception e) { e.printStackTrace(); } } }
NotificationClient C++ クラス・ヘッダ・ファイルnotificationclient.h
では、Android デバイスに通知を表示するためのシンプルな C++ API を宣言しています。これは、単一の文字列プロパティnotification
と、Java コードを呼び出すスロットupdateAndroidNotification()
で構成されています:
class NotificationClient : public QObject { Q_OBJECT public: explicit NotificationClient(QObject *parent = 0); void setNotification(const QString ¬ification); QString notification() const; signals: void notificationChanged(); private slots: void updateAndroidNotification(); private: QString m_notification; };
notificationChanged()
シグナルをupdateAndroidNotification()
スロットに接続し、notification
テキストが変更されたときに通知テキストを更新します:
m_notification = notification; emit notificationChanged();
updateAndroidNotification()
関数は、Android プラットフォーム API から通知を送信する Java メソッドを呼び出します。まず、通知文字列からJava文字列jstring
を構築し、jstring
オブジェクトをJavaのnotify()
メソッドにパラメータとして渡します:
void NotificationClient::updateAndroidNotification() { QJniObject javaNotification = QJniObject::fromString(m_notification); QJniObject::callStaticMethod<void>( "org/qtproject/example/androidnotifier/NotificationClient", "notify", "(Landroid/content/Context;Ljava/lang/String;)V", QNativeInterface::QAndroidApplication::context(), javaNotification.object<jstring>()); }
Javaメソッドの呼び出しでは、Javaと通信するためにJava Native Interface (JNI) APIに依存するQJniObject 。また、前のスニペットでは、アプリのコンテキスト・オブジェクトを渡しています。このコンテキスト・オブジェクトは、静的なJavaコードがアプリ固有のプロパティやAPIを利用するために使用できます。
スマイリーボタンが期待通りに動作することを確認するために、どちらかがクリックされた場合に通知テキストを変更する以下のコードを追加します:
QObject::connect(&happyButton, &QPushButton::clicked, &happyButton, []() { NotificationClient().setNotification("The user is happy!"); }); QObject::connect(&sadButton, &QPushButton::clicked, &happyButton, []() { NotificationClient().setNotification("The user is sad!"); });
Qt for Androidも参照してください 。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.