【发布时间】:2018-07-11 16:58:03
【问题描述】:
我正在尝试使用 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 错误?
【问题讨论】:
标签: android notifications android-service