pendingIntent getBroadcast:检查用户是否已触发状态栏通知

时间:2011-04-06 02:13:40

标签: android

我正在从服务创建状态栏通知,该服务始终在后台运行。 如果用户没有点按5分钟,我想取消通知。

那么如何检查状态栏通知是否仍然有效?

我发现我们可以将pendingIntent getBroadcast与FLAG_NO_CREATE一起使用,如果它返回Non Null则表示状态栏通知仍然有效。但对我来说,即使状态栏通知处于活动状态,alwasy也会返回null。请找到以下代码。

如果我做错了,请告诉我。

代码:

/* 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");

始终打印不存在的通知

1 个答案:

答案 0 :(得分:0)

您必须提供正确的requestCode-value作为PendingIntent.getBroadcast的第二个参数,因此它与创建通知时的值相同。有关详细信息,请参阅此处:http://code.google.com/p/android/issues/detail?id=7780