【问题标题】:Android - notification manager, having a notification without an intentAndroid - 通知管理器,有一个没有意图的通知
【发布时间】:2011-08-12 13:26:23
【问题描述】:

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

我已经尝试为意图传递 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);

【问题讨论】:

  • 只是一个关于这个 null 方法的警告——我也在寻找这个答案。在我的 HTC Desire 上,这会导致设备重启!很糟糕,因为通知是在启动时创建的 - 所以我进入了启动-重启-启动循环......
  • 有趣的是,null 在 4.0 模拟器上也能正常工作。它会在我的 Galaxy S2 (2.3) 上导致 FC。
  • 它真的很有用。非常感谢

标签: android notifications android-intent notificationmanager


【解决方案1】:

你可以传递参数

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

而不是

null

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

【讨论】:

  • 我从广播接收器调用,发现它出现空异常。但是我尝试了我发现的这个课程(有人正在制作一个通知课程)并且它很棒 - 看看stackoverflow.com/questions/5626946/…
  • 这真的很有帮助。我不知道,使用空构造函数创建的 Intent 应该做什么,但对我来说它在 4.0 + 和 2.2 上都能完美运行 :) 谢谢!
  • 非常感谢。你成就了我的一天
【解决方案2】:

setLatestEventInfo() 中的最后一个参数是PendingIntent,而不是Intent。如果您需要在点击时不执行任何操作的通知是传递一个空的 PendingIntent,其完成方式如下:PendingIntent.getActivity(context, 0, null, 0)。

【讨论】:

  • 机器人这个和接受的答案工作得很好,但这个更简单,当你可以通过null时,不需要创建一个空的Intent
  • 传递 null 导致在 android 4.1 中强制关闭
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    相关资源
    最近更新 更多