问题
I make an QT for Android apps. When I click button notify bar seen above in tablet. But if click notify, my apps open/focus(don' know) with black window. How to do it simply. My code is:
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());
}
}
I guest, important code is PendingIntent to open/focus main app. Other case is I don't want to use notify2 as static. Can I use it dynamic from Qt/JNI
回答1:
first way:
you must change this line
intent = new Intent(m_instance, NotificationClient.class);
to
Intent intent = new Intent(mActivityInstance, mActivityInstance.getClass());
and
contentIntent = PendingIntent.getActivity(m_instance, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
to
contentIntent = PendingIntent.getActivity(mActivityInstance, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
attention mActivityInstance is your mainActivity.
I set my mActivityInstance from c++ and with JNI like below:
const QAndroidJniObject m_JavaNotification("com/falsinsoft/qtandroidtools/AndroidNotification",
"(Landroid/app/Activity)V",
QtAndroid::androidActivity().object<jobject>());
and in my AndroidNotification.java:
public class AndroidNotification {
private final Activity mActivityInstance;
public AndroidNotification(Activity ActivityInstance)
{
mActivityInstance = ActivityInstance;
}
}
second way(I dont sure this solve your problem!): change this line in you manifiset.xml:
<application android:hardwareAccelerated="true" android:name="org.qtproject.qt5.android.bindings.QtApplication" android:label="YourProjectName" android:icon="@drawable/icon">
to:
<application android:hardwareAccelerated="true" android:name="your java class extend from QtActivity" android:label="YourProjectName" android:icon="@drawable/icon">
来源:https://stackoverflow.com/questions/37454937/qt-qml-android-app-open-app-when-click-notification-bar