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

Don*_*rty 18 notifications android illegalargumentexception

我有一个运行的服务,当它收到一条消息说它必须被更改时,它会更新通知栏中的通知.

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

java.lang.IllegalArgumentException: contentIntent required
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

变量设置


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

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

NotificationManager mNotificationManager;
Run Code Online (Sandbox Code Playgroud)

NotificationManager创建


    String ns = Context.NOTIFICATION_SERVICE;
    mNotificationManager = (NotificationManager) getSystemService(ns);
Run Code Online (Sandbox Code Playgroud)

通知创建


    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);
Run Code Online (Sandbox Code Playgroud)

更新通知


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

所以有些事情发生在我的内容的某个地方,这是正确的吗?

它在我的Service类的顶部被声明为一个成员变量,并且除了上面显示的代码之外没有在代码中的任何其他地方使用,所以哪里可以重置为null?

nhe*_*eid 15

您需要为通知设置contentIntent.

在你的情况下:

notification.contentIntent = notificationIntent;
Run Code Online (Sandbox Code Playgroud)

否则你会收到消息,通知的contentIntent为null,因为它没有设置.

文档在这里: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

  • 并非所有版本的Android都需要此功能.我有一个案例,除了Kindle Fire之外,其他一切都很好. (5认同)
  • 如果我不想在点击通知时点击任何意图,你知道我做了什么吗?我只是希望它在那里,而系统正在做一些事情,它会自行发展. (4认同)
  • 有趣的是,文档指定需要3件事,但contentIntent不是其中之一.http://developer.android.com/guide/topics/ui/notifiers/notifications.html (2认同)