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)){
}
}