Subscribe: Posts / Comments / Email


RSS

Adobe Air – Saving Preferences

Fri, Sep 19, 2008

Air, Flex, Snippets


flex_save_preferences

Its always good to remember a users preferences, so here is a quick code snip of how to do exactly that.

import com.adobe.air.preferences.PreferenceChangeEvent;
import com.adobe.air.preferences.Preference;

/* Create a new prefernce variable and give it a name */
private var prefs:Preference =  new Preference( "settings.obj" );

/* Create a new bytearry to store the data */
private var byteArray:ByteArray = new ByteArray();

/* Create a new fileStream variable to open up the local file system */
private var fileStream:FileStream;

/* Register some event listeners to see when the prefrences change */
private function init():void
{
	prefs.addEventListener( PreferenceChangeEvent.PREFERENCE_CHANGED_EVENT, onPrefChange );
	checkSettings();
}

/* When they change, set the action to edit, and update the files */
private function onPrefChange( event:PreferenceChangeEvent ):void
{
    if ( event.action == PreferenceChangeEvent.ADD_EDIT_ACTION )
    {

     //The new value
     trace( event.newValue );
     } else if ( event.action == PreferenceChangeEvent.DELETE_ACTION ) {

          //The old value
          trace( event.oldValue );
     }
}            

/* Save the settings, it the checkbox is selected then lets save it */
private function saveSettings():void
{
    if ( cb_remember.selected )
    {
        /* preferences variable, give it a name, set the value, encyrpted or not */
        prefs.setValue( "username", txt_username.text, false );
        prefs.setValue( "password", txt_password.text, true );
        prefs.save();
    } else {
        //Do nothing
    }                

  //Saved or Edited preferences
  trace( prefs );
}        

/* When the application loads, check the settings to see if anything has changed, if it has, set the value of the new values to the values of the inputs */
private function checkSettings():void
{
    prefs.load();                

    txt_username.text = prefs.getValue( "username" );
    txt_password.text = prefs.getValue( "password" );
}

//Clear settings, and start from scratch
private function clearSettings():void
{
    txt_username.text = "";
    txt_password.text = "";
}

Tags: , , ,

4 Comments For This Post

  1. Nicolas Says:

    Is byteArray and fileStream used at all??

  2. Jonnie Says:

    Well, I am sure that they are used, but I haven’t look inside of the adobe air preferences class to see if they use that. I am sure that they do. Make sure you get a copy of those classes from adobe before using this.

  3. increasing youtube views Says:

    I have had difficulties accessing this blog on my phone, why?

  4. Jordan Remmele Says:

    Would it be possible to get permission to use some of your posts on forums with a link? – Offshore Company Formation

Leave a Reply