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
| 归档时间: |
|
| 查看次数: |
15393 次 |
| 最近记录: |