我在安卓上创建了一个简单的便签小部件。我在其中添加了夜间模式功能,使小部件颜色在白天从红色变为黑色。我使用以下代码实现根据时间更改小部件的功能:
// 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 Launcher和Google搜索栏小部件。它会在Android激活夜间模式后切换。有人知道如何做到这一点吗?