我们是从事学校作业的小组。我们已经创建了一个自定义通知,并且效果很好。唯一的问题是,我很难确定如何根据日间或夜间模式更改自定义通知的背景颜色。
我用于创建通知的代码
Intent challengedIntent = new Intent(this, StartActivity.class);
stackBuilder.addNextIntentWithParentStack(challengedIntent);
PendingIntent challengedPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews views = new RemoteViews(getPackageName(), R.layout.notification);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getString(R.string.app_channel))
.setSmallIcon(R.mipmap.carfight_launcher)
.setCustomContentView(views)
.setCustomBigContentView(views)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setContentIntent(challengedPendingIntent)
.setColorized(true)
.setColor(Color.parseColor("#ff0000"));
views.setImageViewResource(R.id.imagesVeiw_wins,R.drawable.ic_medal_solid);
views.setImageViewResource(R.id.imageVeiw_matches,R.drawable.ic_battle);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, builder.build());
我如何更改它以在日/夜模式下做出反应?
minSDKVersion为16
答案 0 :(得分:0)
执行此操作的方法之一是首先确定手机是否处于夜间模式。 可以使用以下代码完成此操作:
int nightModeFlags =
getContext().getResources().getConfiguration().uiMode &
Configuration.UI_MODE_NIGHT_MASK;
switch (nightModeFlags) {
case Configuration.UI_MODE_NIGHT_YES:
setupNightMode();
break;
case Configuration.UI_MODE_NIGHT_NO:
setupDayMode();
break;
case Configuration.UI_MODE_NIGHT_UNDEFINED:
doStuff();
break;
}
然后,一旦知道它是否处于夜间模式,则只需相应地创建RemoteView对象。这意味着您将必须具有两种布局,一种用于普通模式,另一种用于夜间模式。 像这样:
RemoteView remoteView;
//Switch case here
setupNightMode(){
remoteView = new RemoteView() //inflate night mode layout
}
setupDayMode()
{
remoteView = new RemoteView() //inflate day mode layout
}
最后: 构建通知时使用remoteView对象:
NotificationCompat.Builder builder = ...