完整的错误还包含:
android.app.RemoteServiceException: Bad notification for startForeground:我读过其他类似的文章这里,尝试了他们的建议,并阅读了他们的链接,但仍有少数用户报告了这个错误。
概述
活动由外部应用程序启动。此活动启动自定义语音识别服务。It 不使用使用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”操作,因为一旦用户停止通话,通知就会被取消。我最初是传递一个‘空意图’,然而,在阅读了许多帖子后,我添加了显示TTS设置的随机意图/任务,只是为了排除这个问题。
99%的用户对上述代码或传递空意图都没有问题。不过,我需要解决1%的问题,因为这是我应用程序的一个非常重要的部分。
如有任何建议,将不胜感激。
发布于 2012-08-27 08:23:51
在我的例子中,Logcat调试是雄辩的:“android.app.RemoteServiceException: startForeground的错误通知:”第二行读:PendingIntent空!
在Android4.x中,对于启动前台服务来说,拥有一个空PendingIntent并不是一个问题,但是在早期的Android版本中是这样的。
因此,首先仔细检查调试日志(阅读所有的行,为什么不,在这里张贴它们)。如果代码中存在相同的问题,我将检查pendingIntent是否为null。如果mContext为null,可能会发生这种情况!
查看一下http://developer.android.com/reference/android/app/Notification.html#setLatestEventInfo%28android.content.Context,%20java.lang.CharSequence,%20java.lang.CharSequence,%20android.app.PendingIntent%29的文档。
下面是代码的外观(如果您正在从服务中推送这些通知):
// 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官方文档。
关于这上的类似问题,请参见StackOverflow。
https://stackoverflow.com/questions/11265942
复制相似问题