繁体   English   中英

开启省电模式后如何切换到夜间模式?

[英]How to change to Night Mode when the battery saver is turned on?

我正在尝试通过任何可用配置(例如 2.Android Q 强制夜间模式主题上的 1.Battery Saver )制作一个响应夜间模式设置的应用程序。 我基本上已经使用 AppCompatDelegate 来实现模式扩展应用程序。 我听说 AppCompatDelegate.setDefaultNightMode() 重新创建活动并调用 onConfigChange 并且 uimode 已更改,因此下面的代码应重新创建活动。 唯一的问题是它没有。 有人愿意帮忙,卡了很多天。

这是我尝试使用 uiMode 执行此操作的代码。

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:name=".MyApplication"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:configChanges="uiMode"
        android:theme="@style/AppTheme">

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        boolean isDarkThemeOn = isDarkThemeEnabled(getApplicationContext());
        if (isDarkThemeOn){
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        }
        else{
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        }
       // AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
    }
    public boolean isDarkThemeEnabled(Context context){
        return (context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK)
                == Configuration.UI_MODE_NIGHT_YES;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM