我在android中做了一个简单的笔记小部件。我在其中添加了夜间模式功能,其中小部件颜色将从白天的红色变为晚上的黑色。我使用此代码使应用程序根据一天中的时间更改小部件:
// Find Night Mode Automatically//
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
// Checks Whether app Is In Night Mode Or Not//
int currentNightMode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
// Checks All Scenarios//
switch (currentNightMode) {
// Night Mode Is Not Active, We're In Day Time//
case Configuration.UI_MODE_NIGHT_NO: {
// Instantiate Variable RemoteViews remoteViews / Directs Activity To GUI//
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.note_widget_ui);
// Initiate viewText Method//
viewText(context, remoteViews);
// Kill Code//
break;
}
// Night Mode Is Active, We're At Night!//
case Configuration.UI_MODE_NIGHT_YES: {
// Instantiate Variable RemoteViews remoteViews / Directs Activity To GUI//
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.note_widget_night_ui);
// Initiate viewText Method//
viewText(context, remoteViews);
// Kill Code//
break;
}
// We Don't Know What Mode We're In, Assume Notnight//
case Configuration.UI_MODE_NIGHT_UNDEFINED: {
// Instantiate Variable RemoteViews remoteViews / Directs Activity To GUI//
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.note_widget_ui);
// Initiate viewText Method//
viewText(context, remoteViews);
// Kill Code//
break;
}
}
一旦夜间模式开启,如何使用此代码使小部件更新其颜色。另一个例子是 nova 启动器和 google bar 小部件。一旦夜间模式被android激活,它就会切换。有人知道怎么做吗?