开发者

How to persist the camera parameters into SharedPreferences?

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 =开发者_JAVA技巧 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();
     }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜