2012-06-29 60 views
1

完整的錯誤還包括:java.lang.IllegalArgumentException異常:contentIntent需要

android.app.RemoteServiceException: Bad notification for startForeground: 

我讀過其他類似的帖子here,嘗試了他們的建議,並閱讀他們的鏈接,但用戶的少數還在報告這個錯誤。

概述

的活動是由外部應用程序啓動。此活動啓動自定義語音識別服務。它使用startForeground:

this.startService(intent); 

活動然後調用完成();

該服務啓動自定義語音識別類,並在構造函數中將上下文傳遞給它。在「講話的開始檢測」我顯示如下通知:

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設置的random intent/pendingIntent,只是爲了排除這個問題。

99%的用戶對上述代碼或傳遞null意圖都沒有問題。我需要爲1%解決這個問題,因爲它是我應用程序中非常重要的一部分。

任何建議將非常感激。

回答

3

在我的情況下logcat的調試有口才: 「android.app.RemoteServiceException:爲startForeground壞通知:」 第二行寫着:的PendingIntent空

在Android 4.x中有一個null PendingIntent對於啓動前臺服務不是問題,但是在早期的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引用。

對於StackOverflow上的類似問題,另請參閱this

+0

感謝您的回答。這個問題實際上變成了我正在使用的外部庫,所以沒有「正確的」答案。如果您更新了答案,並提供了其他任何閱讀此問題的人的待定意圖鏈接,我會將其標記爲完整性的正確答案。乾杯。 – brandall

+0

好吧,我添加了一些鏈接,最後一個是另一個類似的問題鏈接 - 我複製粘貼我的答覆在這裏以及:D。 – Radu

+0

謝謝,這很有用。我標記爲正確。 – brandall

相關問題