如果默认为自动,如何获取 AppCompatDelegate 当前模式
How to get AppCompatDelegate current mode if default is auto
我有 activity 这样的:
package com.nkdroid.daynighttheme;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.widget.TextView;
public class ModeActivity extends AppCompatActivity {
private TextView txtModeType;
int modeType;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auto_mode);
txtModeType = (TextView) findViewById(R.id.txtModeType);
modeType = AppCompatDelegate.getDefaultNightMode();
if (modeType == AppCompatDelegate.MODE_NIGHT_AUTO) {
txtModeType.setText("Default Mode: Auto");
} else if (modeType == AppCompatDelegate.MODE_NIGHT_YES) {
txtModeType.setText("Default Mode: Night");
} else if (modeType == AppCompatDelegate.MODE_NIGHT_NO) {
txtModeType.setText("Default Mode: Day");
}
}
}`
如果默认模式设置为 AUTO,是否可以获取现在激活的模式(白天或夜晚)?
您可以使用以下代码获取当前模式,
int currentNightMode = getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
// Night mode is not active, we're in day time
case Configuration.UI_MODE_NIGHT_YES:
// Night mode is active, we're at night!
case Configuration.UI_MODE_NIGHT_UNDEFINED:
// We don't know what mode we're in, assume notnight
}
Chris Banes 的以下 article 解释得很好。
如果您是 kotlin 开发者,那么您可以使用下面的代码来检查您的应用程序处于哪种模式..
val mode = context?.resources?.configuration?.uiMode? and Configuration.UI_MODE_NIGHT_MASK
when (mode) {
Configuration.UI_MODE_NIGHT_YES -> {}
Configuration.UI_MODE_NIGHT_NO -> {}
else -> {} //covers Configuration.UI_MODE_NIGHT_UNDEFINED
}
有关深色主题模式的更多信息,请参阅;
当从应用程序内部设置夜间模式时(使用 AppCompatDelegate
),@harshithdwivedi 的回答对我不起作用。否则它工作正常。
所以我不得不像这样添加一些额外的检查:
public static boolean isNightModeActive(Context context) {
int defaultNightMode = AppCompatDelegate.getDefaultNightMode();
if (defaultNightMode == AppCompatDelegate.MODE_NIGHT_YES) {
return true;
}
if (defaultNightMode == AppCompatDelegate.MODE_NIGHT_NO) {
return false;
}
int currentNightMode = context.getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
return false;
case Configuration.UI_MODE_NIGHT_YES:
return true;
case Configuration.UI_MODE_NIGHT_UNDEFINED:
return false;
}
return false;
}
更简单的方法...
val Context.isDarkMode
get() = if (getDefaultNightMode() == MODE_NIGHT_FOLLOW_SYSTEM)
resources.configuration.uiMode and UI_MODE_NIGHT_MASK == UI_MODE_NIGHT_YES
else getDefaultNightMode() == MODE_NIGHT_YES
mode = AppCompatDelegate.getDefaultNightMode();
SharedPreferences preferencesnight = android.preference.PreferenceManager.getDefaultSharedPreferences(MyApplication.this);
if (preferencesnight.getBoolean(KEY_SIGNATURE,false))
{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
else if(mode == AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
{
AppCompatDelegate.setDefaultNightMode(mode);
}
else
{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
我有 activity 这样的:
package com.nkdroid.daynighttheme;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.widget.TextView;
public class ModeActivity extends AppCompatActivity {
private TextView txtModeType;
int modeType;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auto_mode);
txtModeType = (TextView) findViewById(R.id.txtModeType);
modeType = AppCompatDelegate.getDefaultNightMode();
if (modeType == AppCompatDelegate.MODE_NIGHT_AUTO) {
txtModeType.setText("Default Mode: Auto");
} else if (modeType == AppCompatDelegate.MODE_NIGHT_YES) {
txtModeType.setText("Default Mode: Night");
} else if (modeType == AppCompatDelegate.MODE_NIGHT_NO) {
txtModeType.setText("Default Mode: Day");
}
}
}`
如果默认模式设置为 AUTO,是否可以获取现在激活的模式(白天或夜晚)?
您可以使用以下代码获取当前模式,
int currentNightMode = getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
// Night mode is not active, we're in day time
case Configuration.UI_MODE_NIGHT_YES:
// Night mode is active, we're at night!
case Configuration.UI_MODE_NIGHT_UNDEFINED:
// We don't know what mode we're in, assume notnight
}
Chris Banes 的以下 article 解释得很好。
如果您是 kotlin 开发者,那么您可以使用下面的代码来检查您的应用程序处于哪种模式..
val mode = context?.resources?.configuration?.uiMode? and Configuration.UI_MODE_NIGHT_MASK
when (mode) {
Configuration.UI_MODE_NIGHT_YES -> {}
Configuration.UI_MODE_NIGHT_NO -> {}
else -> {} //covers Configuration.UI_MODE_NIGHT_UNDEFINED
}
有关深色主题模式的更多信息,请参阅;
当从应用程序内部设置夜间模式时(使用 AppCompatDelegate
),@harshithdwivedi 的回答对我不起作用。否则它工作正常。
所以我不得不像这样添加一些额外的检查:
public static boolean isNightModeActive(Context context) {
int defaultNightMode = AppCompatDelegate.getDefaultNightMode();
if (defaultNightMode == AppCompatDelegate.MODE_NIGHT_YES) {
return true;
}
if (defaultNightMode == AppCompatDelegate.MODE_NIGHT_NO) {
return false;
}
int currentNightMode = context.getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
return false;
case Configuration.UI_MODE_NIGHT_YES:
return true;
case Configuration.UI_MODE_NIGHT_UNDEFINED:
return false;
}
return false;
}
更简单的方法...
val Context.isDarkMode
get() = if (getDefaultNightMode() == MODE_NIGHT_FOLLOW_SYSTEM)
resources.configuration.uiMode and UI_MODE_NIGHT_MASK == UI_MODE_NIGHT_YES
else getDefaultNightMode() == MODE_NIGHT_YES
mode = AppCompatDelegate.getDefaultNightMode();
SharedPreferences preferencesnight = android.preference.PreferenceManager.getDefaultSharedPreferences(MyApplication.this);
if (preferencesnight.getBoolean(KEY_SIGNATURE,false))
{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
else if(mode == AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
{
AppCompatDelegate.setDefaultNightMode(mode);
}
else
{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}