java.lang.IllegalArgumentException:需要contentIntent

时间:2012-06-29 16:59:20

标签: java android android-notifications

完整错误还包含:

    android.app.RemoteServiceException: Bad notification for startForeground:

我已阅读其他类似帖子here,尝试了他们的建议并阅读了他们的链接,但仍有少数用户报告此错误。

概述

活动由外部应用程序启动。此活动启动自定义语音识别服务。 使用startForeground:

    this.startService(intent);

然后活动调用finish();

该服务启动自定义语音识别类,并在构造函数中将上下文传递给它。在“检测到语音开头”时,我会显示以下通知:

    String notTitle = "Hello";
    String notificationText = "hello there";

    notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    myNotification = new Notification(
                android.R.drawable.ic_btn_speak_now, notTitle,
                System.currentTimeMillis());
    myNotification.flags |= Notification.FLAG_AUTO_CANCEL;

    Intent intent = new Intent();
    intent.setAction("com.android.settings.TTS_SETTINGS");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
        myNotification.contentIntent = pendingIntent;

    myNotification.setLatestEventInfo(mContext, notTitle,
                notificationText, pendingIntent);

    notificationManager.notify(MY_NOTIFICATION_ID, myNotification);

通知没有要求“onClick”做任何事情,因为一旦用户停止说话就会取消。我最初传递了一个'null intent',然而,在阅读了很多帖子之后,我在显示TTS设置的随机意图/ pendingIntent中添加了,只是为了解决这个问题。

99%的用户没有上述代码或传递空意图的问题。我需要为1%解决这个问题,因为它是我应用程序中非常重要的一部分。

非常感谢任何建议。

1 个答案:

答案 0 :(得分:3)

在我的情况下,Logcat调试是雄辩的:“android.app.RemoteServiceException:startForeground的错误通知:” 第二行读取: PendingIntent null

在具有空PendingIntent的Android 4.x中,启动前台服务不是问题,但在早期的Android版本中它是。

首先仔细检查调试日志(阅读所有行,为什么不,请在此处发布)。 如果你的代码中存在同样的问题,我会检查pendingIntent是否为null。如果 mContext 为空,可能会发生这种情况!

查看 setLatestEventInfo 的文档。

以下是您的代码的外观(如果您从服务中推送这些通知):

        // The PendingIntent to launch our activity if the user selects this notification
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, LocalServiceActivities.Controller.class), 0);

    // Set the info for the views that show in the notification panel.
    notification.setLatestEventInfo(this, getText(R.string.local_service_label),
                   text, contentIntent);

代码来自Android official documentation

另请参阅this了解StackOverflow上的类似问题。