gpt4 book ai didi

QT/QML Android 应用,点击通知栏打开应用

转载 作者:行者123 更新时间:2023-12-04 08:49:07 26 4
gpt4 key购买 nike

我为 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());
}
}

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

最佳答案

第一种方式:

你必须改变这一行

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">

关于QT/QML Android 应用,点击通知栏打开应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37454937/

26 4 0