我在Android中创建了一个与此类似的NotificationListenerService code 。我的应用程序在单独的窗口中显示通知。什么时候 用户单击我的窗口中的通知,相应的应用程序将打开。
public void onNotificationPosted(StatusBarNotification sbn) {
Bundle extras = sbn.getNotification().extras;
String title = getStringFromBundle(extras, "android.title");
String subText = getStringFromBundle(extras, "android.subText");
String text = getStringFromBundle(extras, "android.text");
String bigText = getStringFromBundle(extras, "android.bigText");
String array[] = { title, subText, text, bigText };
int progress = extras.getInt("android.progress", 0);
int progressMax = extras.getInt("android.progressMax", 0);
int int_array[] = { progress, progressMax };
notification_added(sbn, array, int_array, bitmap); //Adds the notification in a list
}
我尝试使用 key 打开通知。
public void OpenNotification(String key) {
String keys[] = { key };
StatusBarNotification sbns[] = getActiveNotifications(keys);
for (StatusBarNotification sbn : sbns) {
try {
if (sbn == null) {
Log.i(TAG, "sbn is null");
continue;
}
/*
Notification n = sbn.getNotification();
if (n.contentIntent != null) {
PendingIntent pi = n.contentIntent;
if (pi != null) {
pi.send(this, 0, null);
}
}
*/
cancelNotification(key);
Intent intent = getPackageManager().getLaunchIntentForPackage(
sbn.getPackageName());
if (intent != null) {
Log.i(TAG, "Launching intent " + intent + " package name: "
+ sbn.getPackageName());
}
} catch (Exception e) {
}
}
}
例如,如果单击电子邮件通知,应用程序将启动电子邮件应用程序。但是,它不会打开确切的电子邮件 Activity 。如何从 StatusBarNotification 对象打开 Activity 。
最佳答案
使用按键打开通知。
public void OpenNotification(String key) {
String keys[] = { key };
StatusBarNotification sbns[] = getActiveNotifications(keys);
for (StatusBarNotification sbn : sbns) {
try {
if (sbn == null) {
Log.i(TAG, "sbn is null");
continue;
}
Notification n = sbn.getNotification();
if (n.contentIntent != null) {
PendingIntent pi = n.contentIntent;
if (pi != null) {
pi.send();
}
}
} catch (Exception e) {
}
}
}
关于java - 如何以编程方式在Android中打开(来自NotificationListener Service的StatusBarNotification对象)通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45319871/