【问题标题】:IllegalArgumentException while creating Notification inside a BroadcastReceiver在 BroadcastReceiver 中创建通知时出现 IllegalArgumentException
【发布时间】:2013-03-12 04:00:20
【问题描述】:

我正在使用以下代码在我的 BroadcastReceiveronRecieve 方法内创建一个通知,但它给了我以下异常:

java.lang.IllegalArgumentException: contentIntent required

代码:

 NotificationManager notificationManager;
        notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
    String tickerText;
    String expandedText;
    String expandedTitle;
    int icon;
    long when;
    Notification notification;

    int notificationref = new Random().nextInt(100) + 1;
    icon = R.drawable.reminder;
    tickerText = "New Reminder";
    when = System.currentTimeMillis();
    notification = new Notification(icon, tickerText, when);
    expandedText = "Reminder at: "
            + DateOrTimeString.getTimeString(task.time) + "\n"
            + task.detail;
    expandedTitle = "Reminder:" + task.topic;
    Intent intentDestroyer = new Intent(context, RemindHomeActivity.class);
    intentDestroyer.putExtra("ID", task.id);
    intentDestroyer.putExtra("NOTIFICATIONREF", notificationref);
    launchIntent = PendingIntent.getActivity(context, notificationref,
            intentDestroyer, 0);

    notification.setLatestEventInfo(mContext, expandedTitle, expandedText,
            null);
    notificationManager.notify(1, notification);

此问题仅在 API 级别低于 11 时出现。它在 API 级别 15 Icecream Sandwitch 中起作用

【问题讨论】:

  • 请完整的堆栈跟踪,源代码中有错误行
  • 和其他异常一样,在DDMS中不显示异常的行号

标签: android exception android-notifications


【解决方案1】:

你需要设置contentIntent

void android.app.Notification.setLatestEventInfo(Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent)‬

在你的情况下:

notification.setLatestEventInfo(mContext, expandedTitle, expandedText,
            launchIntent );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 2012-01-27
    • 1970-01-01
    • 2021-06-17
    相关资源
    最近更新 更多