我创建了2个复选框,并将其值保存在共享首选项中的布尔数据类型中。如何在另一个类中获取它并在if-else条件下使用它?
头等舱
@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();
}
和第二类
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)){
}
}
答案 0 :(得分:2)
设置SharedPreferences
SharedPreferences sp =getSharedPreferences("checkbox", MODE_PRIVATE);
SharedPreferences.Editor et = sp.edit();
et.putBoolean("isLogin", true);
et.commit();
从SharedPreferences获取价值
SharedPreferences sp = getSharedPreferences("checkbox", 0);
boolean cb1 = sp.getBoolean("isLogin", false);
答案 1 :(得分:1)
像这样有一百万个questons,只需1分钟谷歌搜索,你就可以实现它。
this is the same question,我复制粘贴解决方案:您可以在任何活动中使用此代码
// 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();
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.
}
更多信息: