我为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动态使用它吗
答案 0 :(得分: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">