2011-12-16 97 views
2

我無法通過自定義佈局顯示通知。如果我使用以下(舊)代碼:Android自定義通知不顯示

notification = new Notification(); 
notification.icon = R.drawable.icon; 
notification.tickerText = currentTickerText; 
notification.when = System.currentTimeMillis(); 
notification.setLatestEventInfo(context, (CharSequence)currentTitle, (CharSequence)"", pendingIntent); 

一切按預期工作,通知顯示出來。但是,如果我使用以下(新)代碼:

contentView = new RemoteViews(context.getPackageName(), R.layout.service_notification); 
contentView.setImageViewResource(R.id.image, R.drawable.icon); 
contentView.setTextViewText(R.id.title, (CharSequence)currentTitle); 
contentView.setTextViewText(R.id.text, (CharSequence)""); 
contentView.setViewVisibility(R.id.pgStatus, View.GONE); 

notification = new Notification(); 
notification.tickerText = currentTickerText; 
notification.when = System.currentTimeMillis(); 

notification.contentView = contentView; 
notification.contentIntent = pendingIntent; 

通知從不顯示。我也沒有給出任何錯誤,所以我不確定在哪裏尋找問題。這裏是service_notification佈局代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10dp" > 
    <ImageView android:id="@+id/image" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_marginRight="10dp" /> 
    <TextView android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/image" 
     android:text="This is the title." 
     style="@style/NotificationTitle" /> 
    <TextView android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/image" 
     android:layout_below="@id/title" 
     android:text="This is the status" 
     style="@style/NotificationText" /> 
    <ProgressBar 
     android:id="@+id/pgStatus" 
     style="?android:attr/progressBarStyleHorizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/text" 
     android:layout_toRightOf="@+id/image" /> 
</RelativeLayout> 

另外,我在Android 2.2模擬器中運行這個,如果有差別。

任何人有什麼想法可能是錯誤的或我怎麼可以去追蹤這個問題?我有點不知所措,因爲我沒有錯誤信息可以解決。謝謝!

回答

11

狀態欄通知要求所有如下:

  • 狀態欄
  • 標題和消息的圖標,除非你定義一個自定義通知佈局
  • 一個的PendingIntent ,當選擇通知時被觸發

from Status Bar Notifications - Creating Notifications

您沒有指定圖標,表示您的通知無效。

(但,是的,這是不好的框架方面,沒有正確的錯誤消息/例外這種情況下)


編輯:通過剛剛測試這一點,我可以確認「隱形「的通知來自Android 2.2的評論。它在2.3.3上工作,看起來像你偶然發現的over a bug

有問題的線是我下面的:

contentView.setViewVisibility(R.id.pgStatus, View.GONE); 

如果我評論說出來,一切正常。當我將android:visibility="gone"添加到XML佈局中的ProgressBar時,它也起作用,它具有基本相同的效果。但只要我在酒吧打電話setViewVisibility(),一切都會分崩離析。

所以我建議使用bugtracker註釋的解決方法,將ProgressBar包裝到LinearLayout中,然後改變它的可見性。

+0

嗯。那麼這至少是問題的一部分。現在,股票代碼通知會顯示,但是當我拖下來查看當前通知時,它說沒有...即使我的圖標清晰地顯示在左上角。思考? – 2011-12-16 11:31:50