StartForeground Bad Notification错误

时间:2012-08-14 07:48:11

标签: android notifications android-service

我正在尝试使用while循环来每秒更新一次通知。但是在2.3.3&下面,它崩溃了这些logcat错误:

08-14 07:30:17.394: E/AndroidRuntime(501): FATAL EXCEPTION: main
08-14 07:30:17.394: E/AndroidRuntime(501): 
android.app.RemoteServiceException: Bad notification for startForeground: 
java.lang.IllegalArgumentException: contentIntent required: 
pkg=com.package.name id=77 
notification=Notification(vibrate=null,sound=null,defaults=0x4,flags=0x40)

问题是即使我检查Build.VERSION,代码崩溃时也会遇到相同的logcat错误:

if (isRunning) {
        n.defaults = Notification.DEFAULT_LIGHTS;
        while (!RecordTimerTask.isRunning && isRunning) {
            long now = System.currentTimeMillis() - startTime;
            n.setLatestEventInfo(getApplicationContext(), getResources()
                    .getString(R.string.notify_title),
                    getDurationBreakdown(now), null);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                startForeground(FOREGROUND_ID, n);
            else 
                notify.notify(ID, n);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

似乎设备运行2.3.3&下面不喜欢null通知意图。 我不明白为什么我从来没有调用过startForeground时遇到logcat错误?

1 个答案:

答案 0 :(得分:4)

问题是你为pendingIntent调用带有null参数的 setLatestEventInfo 。这适用于Android ICS,但不适用于早期版本......

查看 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上的类似问题。