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

发布于 2024-09-07 02:06:37 字数 1477 浏览 7 评论 0原文

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

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

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;

NotificationManager创建


    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 呢?

I have a service running that updates a notification in the notification bar when it recieves a message saying it has to be changed.

However I get the following error sometimes when the notification is to be updated

java.lang.IllegalArgumentException: contentIntent required

Here is my code:

Variable setup


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

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

NotificationManager mNotificationManager;

NotificationManager Creation


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

Notification Creation


    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);

Update of Notification


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

So something is happening my contentIntent somewhere along the line, would that be correct?

It is declared at the top of my Service class as a member variable and is not used anywhere else in the code apart from shown above, so where could it be getting reset to null?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黄昏下泛黄的笔记 2024-09-14 02:06:38

我认为这是因为Android操作系统版本

2.3或更低版本,必须设置contentIntent,如果没有,你会得到这个异常。

在我的项目中,我这样写:

 if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) { 
     Intent intent = new Intent();
     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0); 
     mNotification.contentIntent = contentIntent; 
 }

也许这可以帮助你!

I think this is because the Android OS Version

The version 2.3 or lower,must set contentIntent,if not,you will get this Exception.

In my project,I write like this:

 if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) { 
     Intent intent = new Intent();
     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0); 
     mNotification.contentIntent = contentIntent; 
 }

Perhaps this could help you!

心头的小情儿 2024-09-14 02:06:38

在您的情况下,

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

如果您想使用具有相同操作但不同附加功能的意图:

  1. requestCode from default "0" in

    getActivity(Context context, int requestCode, Intent Intent, int flags)
    

独特的内容,例如 这

(int) System.currentTimeMillis();
notification.contentIntent = notificationIntent;

两个步骤都是强制性的,因为:

  • 如果没有选项 1,选项 2 将不起作用。
  • 如果没有选项 2,选项 1 将抛出 IllegalArgumentException。

In your case

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

if you want to use Intents with the same action but different extras:

  1. Change requestCode from default "0" in

    getActivity(Context context, int requestCode, Intent intent, int flags)
    

to something unique like

(int) System.currentTimeMillis();
notification.contentIntent = notificationIntent;

Both steps are mandatory because:

  • Option 2 will not work without option 1.
  • Option 1 will throw IllegalArgumentException without 2.
我很OK 2024-09-14 02:06:38

就我而言,我有一个示例代码要做,需要创建一个通知,而且我还收到了“contentIntent required”错误 - 谷歌将我​​带到了这个线程:D

这个问题的根源是我从示例中复制的引用代码并将其粘贴到 eclipse 项目中。当我删除“”并重新输入它们时,问题就解决了。也许这对某人有帮助。

这些是错误的引用来源:
nb.setContentTitle("我的第一个通知!");
nb.setContentText("你好");

In my case, I had an example code to do, with a single Notification to create, and I also got "contentIntent required" error - google brought me to this thread :D

the source of this problem were quotations that I copied from an example code and pasted it in eclipse project. When I deleted " " and typed them back and problem was solved. Maybe this helps someone.

These were quotations source of error:
nb.setContentTitle("My first notification!");
nb.setContentText("Hello");

半夏半凉 2024-09-14 02:06:37

您需要为通知设置 contentIntent。

在您的情况下:

notification.contentIntent = notificationIntent;

否则您将收到消息,通知的 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 -从android下载

you need to set the contentIntent for your notification.

in your case:

notification.contentIntent = notificationIntent;

otherwise you will get the message, that the contentIntent of the notification is null, because it's not set.

the docu is here: http://developer.android.com/reference/android/app/Notification.html#contentIntent

i have a little example here: http://united-coders.com/nico-heid/show-progressbar-in-notification-area-like-google-does-when-downloading-from-android

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文