Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.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 - Fatal编程技术网

Android 保存开关状态

Android 保存开关状态,android,Android,你好,我不知道如何让我的应用程序保存切换状态 示例: 我想打开应用程序,我想在重启我的应用程序后关闭开关我想让我的应用程序保持关闭状态,反之亦然 public class AlgeriaNotificationsActivity extends AppCompatActivity { public static final String PREFS_NAME = "MyPrefsFile"; private TextView switchStatus; private Switch mySwi

你好,我不知道如何让我的应用程序保存切换状态
示例:
我想打开应用程序,我想在重启我的应用程序后关闭开关我想让我的应用程序保持关闭状态,反之亦然

public class AlgeriaNotificationsActivity extends AppCompatActivity {

public static final String PREFS_NAME = "MyPrefsFile";
private TextView switchStatus;
private Switch mySwitch;

private static final String TAG = "AlgeriaNotificationsActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_algeria_notifications);

    switchStatus = (TextView) findViewById(R.id.switchStatus);
    mySwitch = (Switch) findViewById(R.id.mySwitch);

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    boolean silent = settings.getBoolean("switchkey", false);
    mySwitch.setChecked(silent);

    mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                                     boolean isChecked) {

            if(isChecked){
                switchStatus.setText("Switch is currently ON");
            }else{
                switchStatus.setText("Switch is currently OFF");
            }
            SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("switchkey", isChecked);
        }
    });

    //check the current state before we display the screen
    if(mySwitch.isChecked()){
        switchStatus.setText("Switch is currently ON");
    }
    else {
        switchStatus.setText("Switch is currently OFF");
    }
}

}

您可以使用SharedReferences进行相同的操作,当切换为check事件发生时,只需将其值保存为true,否则设置为false。

在activity oncreate中,只需检查SharedPreference值是否为true,即可设置开关打开或关闭

开关值的oncreate检查

 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
   boolean silent = settings.getBoolean("switchkey", false);
   mySwitch.setChecked(silent);
开关checkchange中保存状态的代码

 @Override
        public void onCheckedChanged(CompoundButton buttonView,
                                     boolean isChecked) {

            if(isChecked){
                switchStatus.setText("Switch is currently ON");
            }else{
                switchStatus.setText("Switch is currently OFF");
            }
 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putBoolean("switchkey", isChecked);
 editor.commit();
        }

您可以使用SharedReferences进行相同的操作,当切换为check事件发生时,只需将其值保存为true,否则设置为false。

在activity oncreate中,只需检查SharedPreference值是否为true,即可设置开关打开或关闭

开关值的oncreate检查

 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
   boolean silent = settings.getBoolean("switchkey", false);
   mySwitch.setChecked(silent);
开关checkchange中保存状态的代码

 @Override
        public void onCheckedChanged(CompoundButton buttonView,
                                     boolean isChecked) {

            if(isChecked){
                switchStatus.setText("Switch is currently ON");
            }else{
                switchStatus.setText("Switch is currently OFF");
            }
 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putBoolean("switchkey", isChecked);
 editor.commit();
        }

谢谢你的帮助,我不知道该怎么做,请你告诉我“在活动中,一次创建,只需检查共享引用值,如果它为真,则设置打开,否则设置关闭”它仍然没有保存changes@Edward对不起,是我的错误,我无法在onCheckedChanged中提交更改,请检查编辑的答案编辑器。提交();missedglad是否有帮助,如果它对您有效,您可以接受以下回答:)非常感谢它有效,但请确定可以在何处添加此操作FirebaseMessaging.getInstance().unsubscribeFromTopic(“工具”);谢谢你的帮助,我不知道该怎么做,请你告诉我“在活动中,一次创建,只需检查共享引用值,如果它为真,则设置打开,否则设置关闭”它仍然没有保存changes@Edward对不起,是我的错误,我无法在onCheckedChanged中提交更改,请检查编辑的答案编辑器。提交();missedglad是否有帮助,如果它对您有效,您可以接受以下回答:)非常感谢它有效,但请确定可以在何处添加此操作FirebaseMessaging.getInstance().unsubscribeFromTopic(“工具”);