打开省电模式后如何更改为夜间模式?

时间:2020-05-10 07:45:14

标签: android appcompatdelegate

我正在尝试制作一个可以通过任何可用配置响应夜间模式设置的应用 1.打开省电模式 2.Android Q强制夜间模式主题。 我基本上已经使用AppCompatDelegate来实现模式扩展Application。 我听说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;
    }
}

0 个答案:

没有答案