Qt 안드로이드 알리미
안드로이드 애플리케이션에서 Qt로 Java 코드를 호출하는 방법을 보여줍니다.
이 예제는 안드로이드 애플리케이션에 사용자 정의 Java 클래스를 추가하는 방법과 Qt의 JNI 편의 API를 사용하여 이를 호출하는 방법을 보여줍니다.
Android 화면의 상태 표시줄에서 웃는 얼굴 중 하나를 클릭하여 알림을 보냅니다.
예제 실행하기
에서 예제를 실행하려면 Qt Creator에서 Welcome 모드를 열고 Examples 에서 예제를 선택합니다. 자세한 내용은 예제 빌드 및 실행하기를 참조하세요.
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
과 자바 코드를 호출하는 슬롯인 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 네이티브 인터페이스(JNI) API에 의존하는 QJniObject 를 사용하여 Java와 통신합니다. 또한 이전 스니펫에서는 앱의 컨텍스트 객체를 전달하고 있는데, 정적 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도 참고하세요.
© 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.