【问题标题】:Update Widget For Night Mode更新夜间模式的小部件
【发布时间】:2017-01-14 04:09:33
【问题描述】:

我在android中做了一个简单的note 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 启动器和 google bar 小部件。一旦夜间模式被android激活,它就会切换。有人知道怎么做吗?

【问题讨论】:

  • 试试看这个one
  • 为我的活动更改昼夜模式很好。它更新我的主屏幕上的小部件以从红色变为黑色,这就是问题所在。我不知道如何告诉小部件在夜晚或白天到来时自行更新
  • 你制作了颜色xml和公共样式类吗?

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


【解决方案1】:

我让它工作并使用了很多你正在做的事情。我的演示展示了在小部件上工作的昼夜主题。 :) 有一个问题,应用程序必须正在运行才能确定处于何种模式。但如果它是关于不做 auto 则一直有效https://github.com/juanmendez/android-styling-recipes/tree/02.dayNightTheme+Widget

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

【讨论】:

    猜你喜欢
    • 2012-02-03
    • 1970-01-01
    • 2022-11-10
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    相关资源
    最近更新 更多