检查是应用程序通知正在运行
问题描述:
我想检查应用程序通知是否正在运行。我曾尝试过各种illustration。即使通知正在运行,测试也总是返回错误。实施通知为:检查是应用程序通知正在运行
NotificationManager mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.zone_recharge_notification);
remoteViews.setOnClickPendingIntent(R.id.zn_recharge, getPendingSelfIntent(context));
remoteViews.setOnClickPendingIntent(R.id.zn_launch, getLaunchZone(context));
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.zone_holo_logo_48)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContent(remoteViews)
.setAutoCancel(false);
mNotificationManager.notify(AppConstant.RECHARGE_ME_NOTIFICATION_ID, mBuilder.build());
并使用下面的代码段检查它是否正在运行。
public static boolean isNotificationVisible(Context context) {
Intent notificationIntent = new Intent(context, WindowServiceDialog.class);
PendingIntent test = PendingIntent.getService(context, AppConstant.RECHARGE_ME_NOTIFICATION_ID, notificationIntent, PendingIntent.FLAG_NO_CREATE);
return test != null;
}
读this但被在API级别18和我想要的东西,可以在集成电路设备上工作。我怎样才能解决这个问题 ?谢谢。
编辑
未决的每个远程视窗组件
protected PendingIntent getPendingSelfIntent(Context context) {
Intent intent = new Intent(context, AppReceiver.class);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
protected PendingIntent getLaunchZone(Context context) {
Intent intent = new Intent(context, WindowServiceDialog.class);
return PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}
答
随着mBuilder.setDeleteIntent(deletePendingIntent);
(javadoc),您可以设定触发您的通知挂起的意图,在通知是cleard的意图。然后创建一个IntentService(或任何其他的Intent接收器),记录删除并将该信息存储在某处(例如SharedPreferences)。在您的isNotificationVisible
操作中,您可以检查sharedPref中的值。
+0
为什么和谁投票呢?我们在生产中使用它来收集当前显示的通知中的信息... –
'isNotificationVisible()'应该如何工作?你只是创建一个新的PendingIntent实例。 'getService'但是并不意味着它已经存在'PendingIntent',它每次都会创建一个新的实例。 –
并回答您的问题:最好的方法是以不需要知道哪些通知可见的方式设计您的应用。最好只显示通知,如果需要的话,然后忘记它。后来需要引用通知的某种逻辑是一件坏事。但是,如果您确实需要知道通知是否可见和/或通知是什么,那么您始终可以将其自己保存在某个地方,例如在数据库中。 –
从问题上的*线程引用,isNotificationVisible()应该返回true是一个通知正在运行与指定的挂起意图 –