5

我为 Android 应用程序制作了一个 QT。当我单击平板电脑上面看到的按钮通知栏时。但是,如果单击通知,我的应用程序将打开/聚焦(不知道)并带有黑色窗口。如何简单地做到这一点。我的代码是:

package org.ays.operation;

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


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;
     private static PendingIntent contentIntent;
     private static Intent intent;


     public NotificationClient()
     {
         m_instance = this;
     }

     public static  void notify2()
     {
         intent = new Intent(m_instance, NotificationClient.class);
         contentIntent = PendingIntent.getActivity(m_instance, 0, intent,
                                         PendingIntent.FLAG_UPDATE_CURRENT);

         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.setDefaults(Notification.DEFAULT_SOUND);
             m_builder.setContentIntent(contentIntent);
             m_builder.setAutoCancel(true);
         }

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

我来宾,重要的代码是 PendingIntent 打开/聚焦主应用程序。其他情况是我不想将 notify2 用作静态。我可以从 Qt/JNI 动态使用它吗

4

1 回答 1

1

第一种方式:

你必须改变这一行

intent = new Intent(m_instance, NotificationClient.class);

Intent intent = new Intent(mActivityInstance, mActivityInstance.getClass());

contentIntent = PendingIntent.getActivity(m_instance, 0, intent,
                                     PendingIntent.FLAG_UPDATE_CURRENT);

contentIntent = PendingIntent.getActivity(mActivityInstance, 0, intent,
                                     PendingIntent.FLAG_UPDATE_CURRENT);

注意mActivityInstance是你的mainActivity。

我从 C++ 和 JNI 设置我的 mActivityInstance,如下所示:

const QAndroidJniObject m_JavaNotification("com/falsinsoft/qtandroidtools/AndroidNotification",
                   "(Landroid/app/Activity)V",
                   QtAndroid::androidActivity().object<jobject>());

在我的 AndroidNotification.java 中:

public class AndroidNotification {
private final Activity mActivityInstance;

public AndroidNotification(Activity ActivityInstance)
{
    mActivityInstance = ActivityInstance;
}
}

第二种方式(我不确定这能解决你的问题!):在你的 manifiset.xml 中更改这一行:

    <application android:hardwareAccelerated="true" android:name="org.qtproject.qt5.android.bindings.QtApplication" android:label="YourProjectName" android:icon="@drawable/icon">

至:

    <application android:hardwareAccelerated="true" android:name="your java class extend from QtActivity" android:label="YourProjectName" android:icon="@drawable/icon">
于 2020-02-08T11:14:53.053 回答