Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如果默认设置为“自动”,如何获取当前模式_Android_Android Appcompat - Fatal编程技术网

Android 如果默认设置为“自动”,如何获取当前模式

Android 如果默认设置为“自动”,如何获取当前模式,android,android-appcompat,Android,Android Appcompat,我有这样的活动: 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 AppCompatActiv

我有这样的活动:

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");
        }
    }
}`
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;
}

如果默认模式设置为“自动”,是否可以获取当前激活的模式(白天或晚上)?

您可以使用以下代码获取当前模式

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的以下内容很好地解释了这一点。

如果你是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
}
有关黑暗主题模式的更多信息,请参阅


不知何故,当从应用程序内部设置夜间模式时,@harshithdwivedi的答案对我不起作用(使用
AppCompatDelegate
)。否则就可以了

因此,我必须添加一些额外的检查,如:

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");
        }
    }
}`
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;
}