공유 환경 설정에 부울 값을 저장하려고합니다. 그런 다음 부울 값을 문자열로 변환하고 textview를 채 웁니다. 이 코드는 잘 작동합니다. 하지만 에뮬레이터에서 앱을 제거하면 부울 값이 손실됩니다. 그래서 부울을 저장하는 것이 올바른 방법인지 알고 싶습니다.sharedpreferences에 부울을 저장하는 올바른 방법입니까?
public class MainActivity extends Activity implements OnClickListener {
public TextView bool;
public boolean enabled;
public Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences prefs = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
boolean enabled = prefs.getBoolean("key", false);
TextView bool = (TextView) findViewById(R.id.bool);
String theValueAsString = new Boolean(enabled).toString();
bool.setText(theValueAsString);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
changeBoolean();
SharedPreferences prefs = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
boolean enabled = prefs.getBoolean("key", false);
TextView bool = (TextView) findViewById(R.id.bool);
String theValueAsString = new Boolean(enabled).toString();
bool.setText(theValueAsString);
}
public boolean changeBoolean(){
SharedPreferences prefs = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
enabled = true;
prefs.edit().putBoolean("key",enabled).commit();
return enabled;
}
}
감사합니다.
부울을 sharedpreferences에 저장하는 올바른 방법입니까?
응용 프로그램을 다시 설치하면 데이터가 손실됩니다. 올바른가요? (거짓 "키")
부울 = prefs.getBoolean 활성화;
나는이 줄을 unterstand하지 않습니다
왜 거짓입니까? sharedpreferences에 저장할 때 자동으로 변경됩니까?
SharedPreferences는 응용 프로그램에만 "샌드 박스"에서 작동합니다. 응용 프로그램을 삭제/제거하면이 샌드 박스에 저장된 모든 데이터가 삭제됩니다. –
감사합니다. 업데이트를 업로드하면 어떻게됩니까? 업데이트가 안전한가요? – basti12354
예. 응용 프로그램을 업데이트해도 문제가 없습니다. –