更改通知RemoteViews背景颜色

the*_*ani 2 service notifications android themes android-remoteview

我在使用Application-Theme更改Background-Color时遇到问题.

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);
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试过了,但我的通知背景仍然是白色的.ID是正确的,View linLayout是LinearLayout.

请记住:整个代码在服务中调用!

谢谢!

ian*_*ake 6

利用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));
Run Code Online (Sandbox Code Playgroud)

您一定要仔细阅读MediaStyle的所有可用方法,并重新阅读媒体播放I/O 2016演讲中的最佳实践,以获取有关使用通知的示例代码和最佳实践.特别是在谈话的30分钟

  • 你能发布一个示例代码吗?我试图找到一些有用的东西,但例如背景颜色保持黑色 (2认同)

Eri*_*ric 5

setColorized文件说:

设置此通知是否应着色。设置时,使用setColor(int)设置的颜色将用作背景颜色此通知。

这应该仅用于高优先级正在进行的任务,例如导航、正在进行的呼叫或其他类似的用户高优先级事件。

对于大多数款式, 仅当通知用于前台服务通知时才会应用着色。

但是,对于附加了媒体会话的 MediaStyle 和 DecoratedMediaCustomViewStyle 通知,则没有这样的要求。

在 O 之前的任何版本上调用此方法不会对通知产生影响,也不会着色。

科特林代码:

import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat
import androidx.core.app.NotificationCompat
import androidx.media.app.NotificationCompat.DecoratedMediaCustomViewStyle

// create a MediaSession so that we can create a DecoratedMediaCustomViewStyle
val mediaSession = MediaSessionCompat(applicationContext,"tag")
mediaSession.setFlags(0)
mediaSession.setPlaybackState(PlaybackStateCompat.Builder()
        .setState(PlaybackStateCompat.STATE_NONE,0,0f)
        .build())

// create & display the colorized notification
notificationManager.notify(
        0,
        NotificationCompat
                .Builder(this,notificationChannel)
                .setStyle(DecoratedMediaCustomViewStyle()
                        .setMediaSession(mediaSession.sessionToken))
                .setSmallIcon(R.drawable.ic_app_foreground)
                .setColor(getColor(android.R.color.black))
                .setColorized(true)
                .setContentText("Hello, World!")
                .build())

....

// cleanup upon dismissing the notification
mediaSession.release()
Run Code Online (Sandbox Code Playgroud)