如何将相机参数持久化到SharedPreferences中?
如何在活动销毁后保留相机的复选框状态和设置?
或者换句话说,如何将相机参数持久化到SharedPreferences中?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_old);
SharedPreferences preferences =
getSharedPreferences("PREFS_NAME",MODE_WORLD_READABLE);
yourCheckBox = (CheckBox) findViewById( R.id.fonarb );
yourCheckBox.setChecked(preferences.getBoolean("lol",false));
yourCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton yourCheckBox,
boolean isChecked) {
if (isChecked){
Parameters params = camera.getParameters();
params.setColorEffect(Parameters.EFFECT_NEGATIVE);
camera.setParameters(params);
}
else {
Parameters params = camera.getParameters();
params.setColorEffect(Parameters.EFFECT_NONE);
camera.setParameters(params);
}
}
});
public void onStop(){
SharedPreferences settings = getSharedPreferences("PREFS_NAME", MODE_WORLD_READABLE);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("lol", true);
editor.commit();
super.onStop();
}
照你说的做了,不想保存设置! :(
SharedPreferences preferences = getSharedPreferences("qwe",MODE_PRIVATE);
yourCheckBox = (CheckBox) findViewById( R.id.fonarb );
yourCheckBox.setChecked(preferences.getBoolean("lol",false));
yourCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton yourCheckBox,
boolean isChecked) {
if (isChecked){
Parameters params = camera.getParameters();
params.setColorEffect(Parameters.EFFECT_NEGATIVE);
camera.setParameters(params);
}
else {
Parameters params = camera.getParameters();
params.setColorEffect(Parameters.EFFECT_NONE);
camera.setParameters(params);
}
}
});
protected void onPause()
{
SharedPreferences settings = getSharedPreferences("qwe",MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
yourCheckBox = (CheckBox) findViewById( R.id.fonarb );
editor.putBoolean("lol", yourCheckBox.isChecked());
editor.commit();
super.onPause();
}
How to keep checkbox state and settings for camera after the destruction of activity?
Or in other words, how to persist the camera parameters into SharedPreferences?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_old);
SharedPreferences preferences =
getSharedPreferences("PREFS_NAME",MODE_WORLD_READABLE);
yourCheckBox = (CheckBox) findViewById( R.id.fonarb );
yourCheckBox.setChecked(preferences.getBoolean("lol",false));
yourCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton yourCheckBox,
boolean isChecked) {
if (isChecked){
Parameters params = camera.getParameters();
params.setColorEffect(Parameters.EFFECT_NEGATIVE);
camera.setParameters(params);
}
else {
Parameters params = camera.getParameters();
params.setColorEffect(Parameters.EFFECT_NONE);
camera.setParameters(params);
}
}
});
public void onStop(){
SharedPreferences settings = getSharedPreferences("PREFS_NAME", MODE_WORLD_READABLE);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("lol", true);
editor.commit();
super.onStop();
}
Did as you said, does not want to save settings! : (
SharedPreferences preferences = getSharedPreferences("qwe",MODE_PRIVATE);
yourCheckBox = (CheckBox) findViewById( R.id.fonarb );
yourCheckBox.setChecked(preferences.getBoolean("lol",false));
yourCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton yourCheckBox,
boolean isChecked) {
if (isChecked){
Parameters params = camera.getParameters();
params.setColorEffect(Parameters.EFFECT_NEGATIVE);
camera.setParameters(params);
}
else {
Parameters params = camera.getParameters();
params.setColorEffect(Parameters.EFFECT_NONE);
camera.setParameters(params);
}
}
});
protected void onPause()
{
SharedPreferences settings = getSharedPreferences("qwe",MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
yourCheckBox = (CheckBox) findViewById( R.id.fonarb );
editor.putBoolean("lol", yourCheckBox.isChecked());
editor.commit();
super.onPause();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要总是在 SharedPreferences 中存储“true”,只需存储 CheckBox 的当前状态即可。
您还可以考虑将此代码移至 onPause() 而不是 onStop()。操作系统(Honeycomb 之前的版本)可以在活动暂停后终止您的活动,而无需调用 onStop()。如果发生这种情况,您的 SharedPreferences 将不会被保存。
Instead of always storing "true" in the SharedPreferences, simply store the current state of the CheckBox.
You also might consider moving this code to onPause() instead of onStop(). The operating system (pre-Honeycomb) can kill your activity once it has been paused without ever calling onStop(). If that happens, your SharedPreferences will not be saved.