Android StartForeground错误通知错误

Android StartForeground错误通知错误,android,notifications,android-service,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.l

我正在尝试使用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)
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();
            }
        }
问题是,即使我检查Build.VERSION,代码也会崩溃,并出现相同的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)
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及以下版本的设备似乎不喜欢空通知意图。 我不明白的是,为什么在从未调用startForeground时,会出现有关startForeground的logcat错误?

问题是您使用PendingEvent的null参数调用SetLateStevenInfo。这适用于Android ICS,但不适用于早期版本

请查看有关的文档

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

代码引用自

有关StackOverflow的类似问题,请参见。

问题是您使用PendingContent的null参数调用SetLateStevenInfo。这适用于Android ICS,但不适用于早期版本

请查看有关的文档

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

代码引用自


有关StackOverflow的类似问题,请参见。

实际上,如果您不希望处理单击通知的操作,可以使用空的意图,例如:builder.setContentIntent(PendingIntent.getActivity)(this,0,new intent(),0);实际上,如果您不希望处理单击通知的操作,可以使用空的意图,例如:builder.setContentIntent(PendingEvent.getActivity)(this,0,new intent(),0);