如何将相机参数持久化到SharedPreferences中?

发布于 2024-11-17 15:41:29 字数 2423 浏览 2 评论 0原文

如何在活动销毁后保留相机的复选框状态和设置?

或者换句话说,如何将相机参数持久化到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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的笑 2024-11-24 15:41:29

不要总是在 SharedPreferences 中存储“true”,只需存储 CheckBox 的当前状态即可。

SharedPreferences settings = getSharedPreferences("PREFS_NAME", MODE_WORLD_READABLE);
SharedPreferences.Editor editor = settings.edit();
CheckBox yourCheckBox = (CheckBox) findViewById( R.id.fonarb );
editor.putBoolean("lol", yourCheckBox.isChecked());
editor.commit();

您还可以考虑将此代码移至 onPause() 而不是 onStop()。操作系统(Honeycomb 之前的版本)可以在活动暂停后终止您的活动,而无需调用 onStop()。如果发生这种情况,您的 SharedPreferences 将不会被保存。

Instead of always storing "true" in the SharedPreferences, simply store the current state of the CheckBox.

SharedPreferences settings = getSharedPreferences("PREFS_NAME", MODE_WORLD_READABLE);
SharedPreferences.Editor editor = settings.edit();
CheckBox yourCheckBox = (CheckBox) findViewById( R.id.fonarb );
editor.putBoolean("lol", yourCheckBox.isChecked());
editor.commit();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文