I a开发者_如何学Gom creating status bar notification from service which is always running in background. I want to cancel the notification if user has not tapped it for 5 minutes.
So how do I check if status bar notification is still active??
I found out that we can pendingIntent getBroadcast with FLAG_NO_CREATE, if it returns Non Null then it means status bar notification is still active. But for me it alwasy returns null even though status bar notification is active. Please find the code below.
Please let me know if I am doing anything wrong.
Code :
/* Notification intent creation */
Intent notificationIntent = new Intent(Intent.ACTION_MAIN, null);
notificationIntent = new Intent(Intent.ACTION_MAIN, null);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final ComponentName cn = new ComponentName("com.ActivityTrackingUI", "com.ActivityTrackingUI.Main");
notificationIntent.setComponent(cn);
/* check */
if(PendingIntent.getBroadcast( this, 0, notificationIntent, PendingIntent.FLAG_NO_CREATE) != null)
{
Log.e("","notification present");
}
else
Log.e("","notification not present");
always prints notification not present
You have to provide the correct requestCode-value as the 2nd parameter to PendingIntent.getBroadcast, so its the same value as when you created the notification. See also here for details: http://code.google.com/p/android/issues/detail?id=7780
精彩评论