Android - java.lang.IllegalArgumentException:由通知引起的 contentIntent 必需错误?

2024-01-27

我正在运行一项服务,当它收到一条表示必须更改的消息时,它会更新通知栏中的通知。

但是,当要更新通知时,有时会出现以下错误

java.lang.IllegalArgumentException: contentIntent required

这是我的代码:

变量设置


int icon = R.drawable.notification;
CharSequence tickerText = "Test";
long when = System.currentTimeMillis();
PendingIntent contentIntent;

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

NotificationManager mNotificationManager;

通知管理器创建


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

通知创建


    Intent notificationIntent = new Intent(this, TestsApp.class);
    contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.flags |= Notification.FLAG_NO_CLEAR;
    notification.icon = R.drawable.notification3;
    notification.setLatestEventInfo(this, "Registering", "Test", contentIntent);
    mNotificationManager.notify(1, notification);

通知更新


    notification.icon = R.drawable.notification2;
    notification.setLatestEventInfo(getApplicationContext(), "Registered", "Test", contentIntent);
    mNotificationManager.notify(1, notification);   

那么我的 contentIntent 沿线某处发生了一些事情,这是正确的吗?

它在我的 Service 类的顶部声明为成员变量,除了上面显示的内容之外,在代码中的其他任何地方都没有使用它,那么它在哪里可以重置为 null 呢?


您需要为通知设置 contentIntent。

在你的情况下:

notification.contentIntent = notificationIntent;

否则您将收到消息,通知的 contentIntent 为 null,因为它尚未设置。

该文档在这里:http://developer.android.com/reference/android/app/Notification.html#contentIntent http://developer.android.com/reference/android/app/Notification.html#contentIntent

我这里有一个小例子:http://united-coders.com/nico-heid/show-progressbar-in-notification-area-like-google-does-when-downloading-from-android http://united-coders.com/nico-heid/show-progressbar-in-notification-area-like-google-does-when-downloading-from-android

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android - java.lang.IllegalArgumentException:由通知引起的 contentIntent 必需错误? 的相关文章

随机推荐