38

我希望能够触发通知以提醒用户计时器已完成,但是当您单击通知时我不希望有意图。

我已经尝试为意图传入 null

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);

int icon = R.drawable.icon;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();

Notification notification = new Notification(icon, tickerText, when);

CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";

notification.setLatestEventInfo(context, contentTitle, contentText, null);
mNotificationManager.notify(HELLO_ID, notification);
4

3 回答 3

76

你可以传递参数

PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0)

代替

null

notification.setLatestEventInfo(context, contentTitle, contentText, null);

于 2011-08-12T13:59:41.483 回答
9

The last parameter in setLatestEventInfo() is a PendingIntent and not an Intent. If you need the notification to not do anything when tapped is to pass an empty PendingIntent which is done as follows: PendingIntent.getActivity(context, 0, null, 0).

于 2011-08-12T14:05:31.840 回答
1

有趣的问题,我想看看这是否有效。我做了一点挖掘,发现很多人都在问同样的问题。cbursk 似乎找到了一个 hack 来获得这个预期的功能,即将一个 Dialog 传递给通知意图而不是一个 Activity。我猜 Dialog 什么都不做,或者被编程为立即关闭自己,不确定。但我目前正在查看此线程并进行测试。

于 2011-08-12T13:44:27.583 回答