我做了一個應用程序,在Android手機的下拉狀態欄中設置通知。但是,我的代碼中存在一個錯誤(有時會設置通知,有時它們不會)。我希望能夠檢查(在代碼中)如果通知可見用戶。 (即用戶能看到狀態欄中的通知?)。如何檢查哪些通知在Android Dev的狀態欄中處於活動狀態?
我該怎麼做? (提前致謝)。
示例代碼非常感謝。
我做了一個應用程序,在Android手機的下拉狀態欄中設置通知。但是,我的代碼中存在一個錯誤(有時會設置通知,有時它們不會)。我希望能夠檢查(在代碼中)如果通知可見用戶。 (即用戶能看到狀態欄中的通知?)。如何檢查哪些通知在Android Dev的狀態欄中處於活動狀態?
我該怎麼做? (提前致謝)。
示例代碼非常感謝。
我希望能夠檢查(在代碼中)如果通知可見用戶。 (即用戶可以在狀態欄中看到 通知?)。
我該怎麼做?
你不能,對不起。
更新:現在可以與Android 4.3以上版本的http://developer.android.com/reference/android/service/notification/NotificationListenerService.html#getActiveNotifications()
但是,你總是可以簡單地cancel()
它 - 取消Notification
不在屏幕上是完全沒有問題。相反,您可以再次安全地撥打notify()
以獲得相同的Notification
,如果Notification
已經在屏幕上,也不會導致問題。
編輯:
NotificationManager.getActiveNotifications()在API 23加入,如果你不希望使用NotificationListenerService
您需要爲每個通知設置一個ID。
所以你犯了一個通知..
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notId + selectedPosition, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, rightNow.getTimeInMillis() - offset, pendingIntent);
Notification notification = new Notification(R.drawable.icon, "TVGuide Υπενθύμιση", System.currentTimeMillis());
NotificationManager manger = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notification.setLatestEventInfo(context, "Κανάλι: " + b.getString("channel"), "Εκπομπή: " + showname, pendingIntent);
manger.notify(notId, notification);
將其清除..
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,notId, intent, 0);
pendingIntent.cancel();
,並檢查是否活躍。(existAlarm返回null如果沒有可用的未決意圖)
public PendingIntent existAlarm(int id) {
Intent intent = new Intent(this, alarmreceiver.class);
intent.setAction(Intent.ACTION_VIEW);
PendingIntent test = PendingIntent.getBroadcast(this, id + selectedPosition, intent, PendingIntent.FLAG_NO_CREATE);
return test;
}
所以一切都歸結爲初始化每個通知的ID和你如何使它獨特。
@CommonsWare:如果我打電話與一個待定的意圖通知我可以檢查是否存在意圖存在,然後通知也存在 – weakwire 2010-09-02 21:07:37
啊,我明白你要去哪裏。有趣的技術 - 我沒有試過這個或在別處見過它用於這個目的。 – CommonsWare 2010-09-02 21:38:39
很長一段時間...但仍然..它是否適用於後GB版本?如果我使用TaskStackBuilder創建通知,我該如何使用此代碼段? – 2013-05-15 14:43:58
存在一個標誌用於。
Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
FLAG_ONLY_ALERT_ONCE:
......應該,如果你想要的聲音和/或振動每個通知發送的時間,即使它沒有被之前取消播放設置。
雖然,當它再次發送,但不會有任何聲音或振動通知將閃爍。
現在有可能檢查Android 4中未完成的通知。3向上
在這裏看到:
只是把所有在一起。這是如何工作的
要建立一個通知,
Notification n = new Notification.Builder(MyService.this)
.setContentTitle("Notification Title")
.setContentText("Notification Message")
.setSmallIcon(R.drawable.myicon).build();
要通知音呼叫通知的setSound()
,
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification n = new Notification.Builder(MyService.this)
.setContentTitle("Notification Title")
.setContentText("Notification Message")
.setSound(alarmSound)
.setSmallIcon(R.drawable.myicon).build();
要取消通知用戶選擇並啓動了接收意向後, ,請致電setAutoCancel()
,
Notification n = new Notification.Builder(MyService.this)
.setContentTitle("Notification Title")
.setContentText("Notification Message")
.setSound(alarmSound)
.setAutoCancel(true)
.setSmallIcon(R.drawable.myicon).build();
要使特定通知使用Notification.FLAG_ONLY_ALERT_ONCE
只發出一次聲音/振動一次。使用此標誌,您的通知只會發出一次聲音,直到聲明被取消,並且您可以根據通知ID多次調用notify()。請注意,如果您調用cancel()或者用戶取消通知或自動取消,notify()調用將再次發出通知聲音。
n.flags |= Notification.FLAG_ONLY_ALERT_ONCE; // Dont vibrate or make notification sound
最後把通知的通知面板上,
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(notification_id, n);
需要注意的是,如果你想有效地使用通知notification_id
這裏是非常重要的。(保持一個聲音/振動通知或取消特定的通知)。
取消特定的通知,
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(notification_id);
您可以cancel()
即使它不存在,或者您可以撥打notify()
多次,只要你想用相同的ID的通知。請注意,使用不同的ID調用notify會創建新的通知。
所以,不管通知是否存在與否,如果你用正確的notification_id
與Notification.FLAG_ONLY_ALERT_ONCE
標誌設置再打電話notify()
,你可以保持您的通知活着沒有反覆的聲音打擾用戶。
'.setOnlyAlertOnce(true)'爲NotificationCompat – behelit 2017-03-09 22:35:38
的新方法引入到NotificationManager類API 23:
public StatusBarNotification[] getActiveNotifications()
我期待在2019年使用它。 – ZaBlanc 2016-01-21 16:31:42
從API 18開始是不是可用?http://developer.android.com/reference/android/service/notification/NotificationListenerService。 html#getActiveNotifications() – acrespo 2016-03-15 19:28:29
我寫了關於android.app.NotificationManager :: getActiveNotifications而不是NotificationListenerService。 – 2016-03-16 09:12:37
由於Android的棉花糖(API 23),就可以恢復了由您的應用程序活動通知列表。這通知管理器方法是getActiveNotifications()
。這裏更多的信息:https://developer.android.com/reference/android/app/NotificationManager.html#getActiveNotifications()
它seems從Android的M(API 23),可以讓你的過程是怎樣的,如果沒有使用NotificationListenerService,也不需要額外的權限:
notificationManager.getActiveNotifications()
如果您再次致電通知,它將再次寫入通知的初始文本 – weakwire 2010-09-02 21:20:52
@weakwire:僅在通知尚未顯示在屏幕上時。 – CommonsWare 2010-09-02 21:37:31
不知道這一點。不回答這個問題,但用2行代碼來達到目的。 +1 – weakwire 2010-09-02 22:38:37