vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Monday, August 6, 2012

Share date using shared preferences in android application


Here I would like to share the usage of shared preference in android application. Basically in all application we need option to store a common data throughout the application. In android if you such a option then you can use shared preference method.
By using shared preference you can save the data in the three different types of ways.
1. Share data only on single activity
2. Share data on all activities within the application.
3. Share data between the different applications on the device.

First we see the different types of mode used shared preference.

MODE_PRIVATE-This Mode creates the file and it is used within the application.
MODE_WORLD_READABLE- This mode creates the file and it’s readable from other application in the device.
MODE_WORLD_WRITEABLE - This mode creates the file and it’s writable from other application in the device.



Storing Data in Activity Level:
SharedPreferences mypref=getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor= mypref.edit();
        editor.putString("user", "pass");
        editor.commit();

Retrieve the data in same activity:
SharedPreferences mypref=getPreferences(Context.MODE_PRIVATE);
           String val=mypref.getString("user", null);

Storing Data across different activity Level:
Here we can get storage  data from any activity within the application
SharedPreferences mypref= getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=mypref.edit();
        editor.putString("user1", "test1");
        editor.putString("user2", "test2");
        editor.putString("user3", "test3");
        editor.commit();


Retrieve Data across different activity Level::
By using the below code we can get the data from the shared preference.
SharedPreferences mypref= getSharedPreferences("user", Context.MODE_PRIVATE);
           String val=mypref.getString("user1", null);



Store Sharing data across application level :
Here by using the below code if we store the data then we can get the data from the different or another application. This can be achieved by using the package name.
Here I have mention the two application of package name
1. com.sample.demoapp1
2. .com.sample.demoapp2
SharedPreferences mypref= getSharedPreferences("user", Context.MODE_WORLD_READABLE);
        SharedPreferences.Editor editor=mypref.edit();
        editor.putString("user1", "test1");
        editor.commit();

Retrieve Data across application level
Context cont;
                            cont = createPackageContext("com.sample.demoapp1", 0);
                        SharedPreferences mypref=cont.getSharedPreferences("user", Context.MODE_PRIVATE);
                        String x= mypref.getString("user1", null);
                        txt.setText(x);

Here I get the data from the application dempapp1 from demoapp2
Note:For sharing to application level data must store with the mode of Context.MODE_WORLD_READABLE then only it can access from the other application.

If you have any doubts regard using shared preference post your comments here……..

0 comments:

Post a Comment