Access shared preference Boolean value from another class

不羁的心 提交于 2019-12-12 03:09:55

问题


I have Created 2 check boxes and saved their values in boolean data type in shared preferences. How do I get it in another class and use it in if-else conditions?

First Class

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.dash_board);

        onPassCodeListener();

        cb1 = (CheckBox) findViewById(R.id.cb1);
        cb1.setChecked(getfromsp("cb1"));
        cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                switch (buttonView.getId()){
                    case R.id.cb1:
                        saveinsp("cb1",isChecked);
                        break;
                }
            }
        });

        cb2 = (CheckBox) findViewById(R.id.cb2);
        cb2.setChecked(getfromsp("cb2"));
        cb2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                switch (buttonView.getId()){
                    case R.id.cb2:
                        saveinsp("cb2",isChecked);
                        break;
                }
            }
        });


    }

    public Boolean getfromsp(String key){
        sp1 = getApplicationContext().getSharedPreferences("checkbox", android.content.Context.MODE_PRIVATE);
        return sp1.getBoolean(key , false);
    }

    public void saveinsp(String key, Boolean value){
        sp1 = getApplicationContext().getSharedPreferences("checkbox", android.content.Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp1.edit();
        editor.putBoolean(key , value);
        editor.commit();
    }

And Second Class

public class MyReceiver extends BroadcastReceiver {
    SharedPreferences sp1, sp2;


    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle bundle = intent.getExtras();

        if(bundle != null){
            Object[] pdus = (Object[]) bundle.get("pdus");

            for (int i = 0; i < pdus.length ; i++) {
                SmsMessage sms = SmsMessage.createFromPdu((byte[]) pdus[i]);
                String message = sms.getMessageBody();

                Toast.makeText(context, "Broadcast received", Toast.LENGTH_SHORT).show();
            }
        }
        sp2 = context.getSharedPreferences("passcode", Context.MODE_PRIVATE);
        String pass = sp2.getString("passcode","");
        sp1 = context.getSharedPreferences("checbox",Context.MODE_PRIVATE);
        Boolean cb1 = sp1.getBoolean("cb1", )
        if(bundle.equals(pass)){

        }


    }

回答1:


Setting SharedPreferences

SharedPreferences sp =getSharedPreferences("checkbox", MODE_PRIVATE);
SharedPreferences.Editor et = sp.edit();
et.putBoolean("isLogin", true);
et.commit();

Getting value from SharedPreferences

SharedPreferences sp = getSharedPreferences("checkbox", 0);
boolean cb1 = sp.getBoolean("isLogin", false);



回答2:


there are like one million of questons like this, and with 1 minute of google search you would have achieved it.

this is the same question, I copy-paste the solution: you can use this code in any activity

Setting values in Preference:

// MY_PREFS_NAME - a static String variable like: 
//public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
 editor.putString("name", "Elena");
 editor.putInt("idName", 12);
 editor.commit();

Retrieve data from preference:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
  String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
  int idName = prefs.getInt("idName", 0); //0 is the default value.
}

more info:

Using Shared Preferences

Shared Preferences



来源:https://stackoverflow.com/questions/39594687/access-shared-preference-boolean-value-from-another-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!