2016-06-24 148 views
0

我用Application-Theme更改背景顏色時遇到問題。更改通知RemoteViews背景顏色

NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this); 

TypedValue typedValue = new TypedValue(); 

getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true); 

int iPrimaryColor = typedValue.data; 

getTheme().resolveAttribute(R.attr.colorPrimaryDark, typedValue, true); 

int iPrimaryDarkColor = typedValue.data; 

Intent notIntent = new Intent(getApplicationContext(), MainActivity.class); 
notIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

PendingIntent notOpenOnClick = PendingIntent.getActivity(getApplicationContext(), 0, notIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

RemoteViews smallContentView = new RemoteViews(getPackageName(), R.layout.notification_small); 
RemoteViews bigContentView = new RemoteViews(getPackageName(), R.layout.notification_expanded); 

nBuilder.setSmallIcon(R.drawable.not_icon) 
    .setOngoing(true) 
    .setContentTitle(getCurrentSong().getTitle()) 
    .setContentIntent(notOpenOnClick); 

Notification not = nBuilder.build(); 

smallContentView.setInt(R.id.not_linLayout, "setBackgroundColor", iPrimaryColor); 
smallContentView.setInt(R.id.not_imvDivider, "setBackgroundColor", iPrimaryDarkColor); 

bigContentView.setInt(R.id.not_linLayout, "setBackgroundColor", iPrimaryColor); 
bigContentView.setInt(R.id.not_imvDivider, "setBackgroundColor", iPrimaryDarkColor); 

setListeners(smallContentView); 
setListeners(bigContentView); 

not.contentView = smallContentView; 
not.bigContentView = bigContentView; 

if (isPlaying()) { 
    not.contentView.setImageViewResource(R.id.not_btnPlayPause, R.drawable.ic_pause_48dp); 
    not.bigContentView.setImageViewResource(R.id.not_btnPlayPause, R.drawable.ic_pause_48dp); 
} 
else { 
    not.contentView.setImageViewResource(R.id.not_btnPlayPause, R.drawable.ic_play_48dp); 
    not.bigContentView.setImageViewResource(R.id.not_btnPlayPause, R.drawable.ic_play_48dp); 
} 

我已經試過這個,但是我的通知背景仍然是白色的。 這個ID是正確的,View linLayout是一個LinearLayout。

請注意:整個代碼是在服務中調用的!

謝謝!

+0

它看起來像你試圖建立一個媒體控制通知。請不要使用自定義通知,而是使用[NotificationCompat.MediaStyle](https://developer.android.com/reference/android/support/v7/app/NotificationCompat.MediaStyle.html) – ianhanniballake

+0

好的,但爲什麼使用MediaStyle會更好嗎? –

+0

1)適用於API 7+設備2)在棒棒糖上使用內置樣式+ 3)自動適應新的Android N通知樣式4)內置支持自定義背景顏色5)內置支持解決問題解除前棒棒糖設備上前臺服務的通知6)支持添加MediaSession標記以使Android Wear控件正常工作。僅舉幾例。 – ianhanniballake

回答

4

利用NotificationCompat.MediaStyle可以輕鬆完成大部分操作。它從pre-API 24設備上調用setColor()調用的背景顏色(並將該顏色用作API 24+設備上的重音)。這也意味着,你不必再編寫任何自定義RemoteViews代碼,它完全依賴於你添加到您的通知媒體控制的行動:

NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this); 
nBuilder.setSmallIcon(R.drawable.not_icon) 
    .setContentTitle(getCurrentSong().getTitle()) 
    .setContentIntent(notOpenOnClick); 
// This is what sets the background color on <N devices 
// It is an accent color on N+ devices 
nBuilder.setColor(getResources().getColor(R.color.colorPrimary)); 
// Add actions via nBuilder.addAction() 
// Set the style, setShowActionsInCompactView(0) means the first 
// action you've added will be shown the non-expanded view 
nBuilder.setStyle(new NotificationCompat.MediaStyle() 
    .setShowActionsInCompactView(0)); 

你一定要讀了所有的方法可供MediaStyle並回顧了有關使用通知的示例代碼和最佳實踐Best Practices in media playback I/O 2016 talk。具體在30 minutes into the talk

+0

你能發表一個示例代碼嗎?我試圖找到有用的東西,但例如背景顏色保持黑色 –

+0

okey謝謝:) –