Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use SharedPrefrence in Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 310
    Comment on it

    SharedPrefrence help us to store private primitive app data in form of key value pairs, but the data is lost when you uninstall the application or you clear application data through settings.To use the SharedPrefrence in your android app follow the steps mentioned below:-

    1) Create a java file named SharedPrefrenceManager.

    SharedPrefrenceManager.java

    import android.content.Context;
        import android.content.SharedPreferences;
    
        public class SharedPreferenceManager 
    
              {
    
            final static String ZIP_FILE_NAME="zipfilename";
    
            private SharedPreferenceManager()
                {
                }
            private static SharedPreferenceManager manager;
            public static SharedPreferenceManager getInstance()
                {
                    if(manager==null)
                    {
                        manager=new SharedPreferenceManager();
                    }
                    return manager;
                }
    
    
                                           //Method to save zip File Name 
    
                   public void saveZipfilename(String zipfilename,Context context)
                {
                    SharedPreferences preferences=(SharedPreferences)context.getSharedPreferences(SharedPreferenceManager.ZIP_FILE_NAME, 0);
                    SharedPreferences.Editor editor=preferences.edit();
                    editor.putString("zipfilename", zipfilename);
                    editor.commit();
                }
                                          //Method to retrieve Zip File Name
    
                public static String getZipfilename(Context context){
                    SharedPreferences preferences=(SharedPreferences)context.getSharedPreferences(SharedPreferenceManager.ZIP_FILE_NAME, 0);
                    return preferences.getString("zipfilename", "");
                }
    
           }
    

    2) In this example we are saving and retrieving Zipfile name so here is the way to save the Zip File Name in SharedPrefrence

    Date date = new Date();
            String format = "yyMMddHHmm";
            SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
            String dateString = sdf.format(date);
            String BluetoothName=bluetoothDevice.getName().toString();
            String fileName = BluetoothName+"-"+ dateString;
        SharedPreferenceManager sharedPreferenceManager = SharedPreferenceManager.getInstance();  
               //calling saveZipfilename   Method     
            sharedPreferenceManager.saveZipfilename(fileName, getActivity());   
    

    3) Below is the way to retrieve the saved zip file name.

    if(zipname==null)                                                                                                       
            {
            SharedPreferenceManager sharedPreferenceManager = SharedPreferenceManager.getInstance();
                zipname=SharedPreferenceManager.getZipfilename(UploadWeldService.this);                  //retrieving zipfilename 
            }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: