Á¤º¸ ÀúÀå ¹× ºÒ·¯¿À±â

  Á¤º¸ ÀúÀå ¹× ºÒ·¯¿À±â ±â´ÉÀ» ±¸ÇöÇϱâÀ§ÇØ ¿©·¯ °¡Áö°¡ ÀÖ°ÚÀ¸³ª °¡Àå °£´ÜÇÑ °ÍÀÌ SharedPreferences ÀÌ´Ù.

  SharedPreferences¸¦ »ç¿ëÇϸ頳»ºÎÀûÀ¸·Î ÆÄÀϷΠÀúÀåµÇ¾î AppÀÌ Á¾·áÇÏ¿©µµ À¯ÁöµÇ¸ç, ¸¸¾à AppÀ» »èÁ¦Çϸ頰°ÀÌ »èÁ¦µÇ°Ô µÇ´Â Á¤º¸ÀÌ´Ù.

  ¿©±â¼­´Â ÀÌ SharedPreferences ¿¡ ´ëÇØ ¾Ë¾Æº¸°Ú´Ù.

¸ÕÀú SharedPreferencesSample¶ó´Â ÇÁ·ÎÁ§Æ®¸¦ ¸¸µé¾î ÇÁ·ÎÁ§Æ®¿¡¼­ ¸¶¿ì½º ¿ìÃø¹öưÀ» Ŭ¸¯ÇÏ¿© »ý¼ºµÈ ÆË¾÷¸Þ´ºÀÇ AddÀÇ ºÎ¸Þ´º Class¸¦ ¼±ÅÃÇÏ¸é ´ÙÀ½°ú °°Àº ´ëÈ­ »óÀÚ°¡ »ý¼ºµÈ´Ù.

Name¿¡ Setting.cs¶ó°í ÀÔ·ÂÇϰí Add¸¦ ´©¸£¸é SettingÀ̶ó´Â Ŭ·¡½º°¡ »ý¼ºµÈ´Ù.

ÀÌ Å¬·¡½º¿¡ LoadÇÔ¼ö¿Í SaveÇÔ¼ö¸¦ ¸¸µç ¼Ò½º´Â ´ÙÀ½°ú °°´Ù.

using System;

using Android.App;

using Android.Content;

 

namespace SharedPreferencesSample

{

    public class Setting

    {

        public int alarmKind = 0;      //0 : SOUND, 1:VIBRATOR, 2:VIBRATOR AND SOUND

        public String strSoundFile;

        public int nVibratorTime = 30;

        public int nDistance = 700;

 

        public void Load()

        {

            ISharedPreferences prefs = Application.Context.GetSharedPreferences("setting", FileCreationMode.Private);

 

            this.alarmKind = prefs.GetInt("AlarmKind", 1);

            this.strSoundFile = prefs.GetString("SoundFile", "");

            this.nVibratorTime = prefs.GetInt("VibratorTime", 30);

            this.nDistance = prefs.GetInt("Distance", 700);

        }

 

        public void Save()

        {

            ISharedPreferences prefs = Application.Context.GetSharedPreferences("setting", FileCreationMode.Private);

            ISharedPreferencesEditor editor = prefs.Edit();

 

            editor.PutInt("AlarmKind", this.alarmKind);

            editor.PutString("SoundFile", this.strSoundFile);

            editor.PutInt("VibratorTime", this.nVibratorTime);

            editor.PutInt("Distance", this.nDistance);

            editor.Apply();

        }

    }

}

¿ì¼± ÀúÀåÇÒ Á¤º¸¿¡ ÇØ´çÇÏ´Â º¯¼ö¸¦ Á¤ÀÇÇÑ´Ù. ¿©±â¼­´Â Sample·Î ¾Æ·¡ 4°³ÀÇ º¯¼ö¸¦ ¼±¾ðÇß´Ù.

        public int alarmKind = 0;      //0 : SOUND, 1:VIBRATOR, 2:VIBRATOR AND SOUND

        public String strSoundFile;

        public int nVibratorTime = 30;

        public int nDistance = 700;

Load ÇÔ¼ö¸¦ ¸¸µé°í ISharedPreferences¸¦ settingÀ̶ó´Â À̸§À¸·Î »ý¼º(ISharedPreferences prefs = Application.Context.GetSharedPreferences("setting", FileCreationMode.Private);)ÇÑ ÈÄ ¼±¾ðÇÑ º¯¼ö prefs¸¦ ÀÌ¿ëÇÏ¿© Á¤º¸¸¦ Àд´Ù.

        public void Load()

        {

            ISharedPreferences prefs = Application.Context.GetSharedPreferences("setting", FileCreationMode.Private);

 

            this.alarmKind = prefs.GetInt("AlarmKind", 1);

            this.strSoundFile = prefs.GetString("SoundFile", "");

            this.nVibratorTime = prefs.GetInt("VibratorTime", 30);

            this.nDistance = prefs.GetInt("Distance", 700);

        }

»ç¿ë°¡´ÉÇÔ¼ö´Â GetBoolean, GetFloat, GetInt, GetLong, GetString µîÀÌ ÀÖ´Ù.

ù ¹øÂ° ÀÎÀÚ´Â ±¸ºÐÇÏ´Â ¹®ÀÚ¿­ÀÌ¸ç µÎ ¹øÂ° ÀÎÀÚ´Â Çѹøµµ ÀúÀåµÇÁö ¾Ê¾ÒÀ» ¶§ ¼³Á¤µÉ ±âº»°ªÀÌ´Ù.

 

Save ÇÔ¼ö¸¦ ¸¸µé°í ISharedPreferences¸¦ settingÀ̶ó´Â À̸§À¸·Î »ý¼ºÇϰí, »ý¼ºµÈ º¯¼ö·ÎºÎÅÍ editor º¯¼ö¸¦ ¾òÀº(ISharedPreferencesEditor editor = prefs.Edit();) ÈÄ ÀÌ º¯¼ö¸¦ ÀÌ¿ëÇÏ¿© Á¤º¸¸¦ ÀúÀåÇÑ´Ù.

        public void Save()

        {

            ISharedPreferences prefs = Application.Context.GetSharedPreferences("setting", FileCreationMode.Private);

            ISharedPreferencesEditor editor = prefs.Edit();

 

            editor.PutInt("AlarmKind", this.alarmKind);

            editor.PutString("SoundFile", this.strSoundFile);

            editor.PutInt("VibratorTime", this.nVibratorTime);

            editor.PutInt("Distance", this.nDistance);

            editor.Apply();

        }

»ç¿ë°¡´ÉÇÔ¼ö´Â PutBoolean, PutFloat, PutInt, PutLong, PutString µîÀÌ ÀÖ´Ù.

ù ¹øÂ° ÀÎÀÚ´Â ±¸ºÐÇÏ´Â ¹®ÀÚ¿­ÀÌ¸ç µÎ ¹øÂ° ÀÎÀÚ´Â ÀúÀåµÉ °ªÀÌ´Ù.

¸¶Áö¸·À¸·Î editor.Apply()¸¦ È£ÃâÇÏ¸é ½ÇÁ¦ ÀúÀåÀÌ µÈ´Ù.

´ÙÀ½Àº ÀÛ¼ºÇÑ SettingÀ» ¼±¾ðÇÏ¿© Load¿Í Save¸¦ È£ÃâÇÑ ¿¹ÀÌ´Ù.

¿©±â¼­´Â ¿¹¸¦ º¸¿©ÁÖ±â À§ÇØ MainActivity¸¦ ¸¸µé¶§ setting Á¤º¸¸¦ LoadÇϰí Back۸¦ ´­·¯ MainActivity¸¦ Á¾·áÇÒ ¶§ setting Á¤º¸¸¦ SaveÇß´Ù. ±×·¯³ª ½ÇÁ¦ »ç¿ë¿¡¼­´Â Back۸¦ ´­·¶À» ¶§°¡ ¾Æ´Ï¶ó ¼³Á¤À» ÇßÀ» ¶§ ÀúÀåÇÏ´Â °ÍÀÌ ÀϹÝÀûÀÌ´Ù.

 

´Ù¿î·Îµå