如何使用共享首选项来保存ToggleButton / Switch状态?

时间:2016-08-03 06:18:52

标签: android android-switch android-togglebutton

我已经成功地向我的应用程序实施了Firebase推送通知,但现在我希望ToggleButton / Switch存储用户选择,我的意思是,当用户激活交换机时我想要存储该状态,因为当用户关闭应用程序,状态自动更改为“已禁用”,这是我的代码:

我已经尝试了一些教程,但我无法解决这个问题......

package com.lfcchile;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;

import com.google.firebase.messaging.FirebaseMessaging;

/**
 * Created by Jona on 7/25/16.
 */
public class NotificationSettings extends AppCompatActivity {

    private static final String TAG = "FCM Service";
    private Switch switchComunidad, switchBlog, switchEquipo, switchPartidos;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.notification_settings);

        switchComunidad = (Switch) findViewById(R.id.switchComunidad);
        switchBlog = (Switch) findViewById(R.id.switchBlog);
        switchEquipo = (Switch) findViewById(R.id.switchEquipo);
        switchPartidos = (Switch) findViewById(R.id.switchPartidos);

        switchComunidad.setTextOn("On");
        switchComunidad.setTextOff("Off");
        switchBlog.setTextOn("On");
        switchBlog.setTextOff("Off");
        switchEquipo.setTextOn("On");
        switchEquipo.setTextOff("Off");
        switchPartidos.setTextOn("On");
        switchPartidos.setTextOff("Off");

        //Funciones que controlan las acciones de cada Switch
        switchComunidad.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (switchComunidad.isChecked()){
                    FirebaseMessaging.getInstance().subscribeToTopic("Comunidad");
                    Log.d(TAG, "Suscrito al tema Comunidad");
                    Toast.makeText(getApplicationContext(), "Activado Correctamente",
                            Toast.LENGTH_LONG).show();
                }else {
                    FirebaseMessaging.getInstance().unsubscribeFromTopic("Comunidad");
                    Log.d(TAG, "Suscrito al tema Comunidad");
                    Toast.makeText(getApplicationContext(), "Desactivado Correctamente",
                            Toast.LENGTH_LONG).show();
                }
            }
        });

        switchBlog.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (switchBlog.isChecked()){
                    FirebaseMessaging.getInstance().subscribeToTopic("Blog");
                    Toast.makeText(getApplicationContext(), "Activado Correctamente",
                            Toast.LENGTH_LONG).show();
                }else {
                    FirebaseMessaging.getInstance().unsubscribeFromTopic("Blog");
                    Toast.makeText(getApplicationContext(), "Desactivado Correctamente",
                            Toast.LENGTH_LONG).show();
                }
            }
        });

        switchEquipo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (switchEquipo.isChecked()){
                    FirebaseMessaging.getInstance().subscribeToTopic("Equipo");
                    Toast.makeText(getApplicationContext(), "Activado Correctamente",
                            Toast.LENGTH_LONG).show();
                }else {
                    FirebaseMessaging.getInstance().unsubscribeFromTopic("Equipo");
                    Toast.makeText(getApplicationContext(), "Desactivado Correctamente",
                            Toast.LENGTH_LONG).show();
                }
            }
        });

        switchPartidos.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (switchPartidos.isChecked()){
                    FirebaseMessaging.getInstance().subscribeToTopic("Partidos");
                    Toast.makeText(getApplicationContext(), "Activado Correctamente",
                            Toast.LENGTH_LONG).show();
                }else {
                    FirebaseMessaging.getInstance().unsubscribeFromTopic("Partidos");
                    Toast.makeText(getApplicationContext(), "Desactivado Correctamente",
                            Toast.LENGTH_LONG).show();
                }
            }
        });

    }
}

提前致谢!

1 个答案:

答案 0 :(得分:0)

我为你构建了一个简化的例子。

private static final String SWITCH_PARTIDOS_STATE = "switchPartidosState";
private SharedPreferences sharedPreferences;
private Switch switchPartidos;  

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sharedPreferences = getSharedPreferences("myname", Context.MODE_PRIVATE);

    switchPartidos.setChecked(sharedPreferences.getBoolean(SWITCH_PARTIDOS_STATE, false));

    switchPartidos.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            sharedPreferences.edit().putBoolean(SWITCH_PARTIDOS_STATE, isChecked).commit();
            if (isChecked){
                FirebaseMessaging.getInstance().subscribeToTopic("Partidos");
                Toast.makeText(getApplicationContext(), "Activado Correctamente",
                        Toast.LENGTH_LONG).show();
            }else {
                FirebaseMessaging.getInstance().unsubscribeFromTopic("Partidos");
                Toast.makeText(getApplicationContext(), "Desactivado Correctamente",
                        Toast.LENGTH_LONG).show();
            }
        }
    });
}

希望它有所帮助!

相关问题