【问题标题】:NotificationManager.notify throws IllegalArgumentExceptionNotificationManager.notify 抛出 IllegalArgumentException
【发布时间】:2012-11-22 20:40:04
【问题描述】:

我有一个NotificationManager,它成功地创建了一个Notification

 private void showNotification() {
    Notification notification = new Notification(R.drawable.snog_icon, getString(R.string.sn_g_entering_beacon_mode_),
            System.currentTimeMillis());

    // The PendingIntent to launch our activity if the user selects this notification
    Intent i = new Intent(this, SnogActivity.class);
    i.putExtra("fromNotification", "yes");

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, 0);

    notification.setLatestEventInfo(this, getString(R.string.sn_g_avalanche_buddy),
                   getString(R.string.beacon_mode_activated_), contentIntent);
    notification.flags |= Notification.FLAG_ONGOING_EVENT; // Notification.DEFAULT_ALL 

    // Send the notification.
    // We use a string id because it is a unique number.  We use it later to cancel.
    mNM.notify(R.string.service_started, notification);
}

这部分工作正常,它显示了我的通知,当我点击通知时,正确的活动开始了。稍后在应用程序中,我尝试通知一个简单的通知:

Notification not = new Notification(R.drawable.snog_icon, "checker", System.currentTimeMillis());
not.flags |= Notification.DEFAULT_ALL;

mNM.notify(R.string.checker, not);

这会使应用程序在notify() 调用中以IllegalArgumentException 崩溃。根据相当多的互联网结果,我应该使用NotificationCompat.Builder,但这甚至不可用。

【问题讨论】:

    标签: android notifications


    【解决方案1】:

    您可以修复此异常,但我宁愿从一开始就使用 NotifcationCompat。它在兼容性包中可用,您必须通过右键单击您的 Eclipse 项目 > Android 工具 > 添加支持库来添加

    在此之后,您将在项目中使用 NotificationCompat...然后访问该站点:http://developer.android.com/guide/topics/ui/notifiers/notifications.html

    那里有一些伟大而简单的例子。

    祝你好运!

    【讨论】:

    • 我的 Eclipse 安装中一定有什么问题,它没有加载支持库....
    • 试图在SDK Manager中找到它:没有,在SDK文件夹中:没有。
    猜你喜欢
    • 2019-06-04
    • 2014-09-18
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多