更新夜间模式小部件

时间:2017-01-14 04:09:33

标签: android android-widget android-night-mode

我在android中做了一个简单的注释Widget。我在其中添加了夜间模式功能,其中小部件颜色将从白天的红色变为夜晚的黑色。我使用此代码使应用程序根据一天中的时间更改小部件:

    // 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启动器和谷歌小部件。它切换一次夜间模式由android激活。有谁知道怎么做?

1 个答案:

答案 0 :(得分:0)

我开始工作并使用了很多你正在做的事情。我的演示显示了在小部件上工作的日夜主题。 :)有一个问题,应用程序必须运行才能确定所使用的模式。但是,如果它不是在做自动,那么https://github.com/juanmendez/android-styling-recipes/tree/02.dayNightTheme+Widget

一直在工作

这是半个答案。这是一个完整的答案。我在与应用程序一起使用时编写了一个库,然后它需要您需要包含的某些依赖项。然后它使用您的依赖项并允许您的WidgetProvider将小部件更新为白天或夜晚主题。 https://github.com/juanmendez/LightThemeScheduler

相关问题