In this tutorial I'm going to illustrate how we can share data between two Android applications using Shared Preference.
To implement this I used two Android applications. One is "Datawriter" and the other one is "Datareader".
"Datawriter" is to update shared data. Its' package name is com.writer.data class name is DataWriterActivity . Here is the code for DataWriterActivity class.
dataWriter method will write the string Hello! this is shared data to a shared memory.
Next application is to read shared data. The application name is Datareader and its' package name is com.datareader class name is DataReaderActivity. Here is the code for DataReaderActivity class.
"com.writer.data" in the highlighted line is the package name of the first application which we used to share data.
Following is the out put of second application :
To implement this I used two Android applications. One is "Datawriter" and the other one is "Datareader".
"Datawriter" is to update shared data. Its' package name is com.writer.data class name is DataWriterActivity . Here is the code for DataWriterActivity class.
| 
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 | packagecom.writer.data;importandroid.app.Activity;importandroid.content.Context;importandroid.content.SharedPreferences;importandroid.os.Bundle;publicclassDataWriterActivity extendsActivity {    /** Called when the activity is first created. */    @Override    publicvoidonCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        dataWriter();    }        publicvoiddataWriter(){        String strShareValue = "Hello! this is shared data";        SharedPreferences prefs = getSharedPreferences("demopref",Context.MODE_WORLD_READABLE);        SharedPreferences.Editor editor = prefs.edit();        editor.putString("demostring", strShareValue);        editor.commit();    }} | 
dataWriter method will write the string Hello! this is shared data to a shared memory.
Next application is to read shared data. The application name is Datareader and its' package name is com.datareader class name is DataReaderActivity. Here is the code for DataReaderActivity class.
| 
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 | packagecom.datareader;importandroid.app.Activity;importandroid.content.Context;importandroid.content.SharedPreferences;importandroid.content.pm.PackageManager.NameNotFoundException;importandroid.os.Bundle;importandroid.util.Log;importandroid.widget.TextView;publicclassDataReaderActivity extendsActivity { String dataShared;    /** Called when the activity is first created. */    @Override    publicvoidonCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);         dataRead();         TextView tv = (TextView)findViewById(R.id.textView1);         tv.setText(dataShared);            }        publicvoiddataRead(){      Context con;         try{             con = createPackageContext("com.writer.data", 0);             SharedPreferences pref = con.getSharedPreferences("demopref", Context.MODE_PRIVATE);             dataShared = pref.getString("demostring", "No Value");         }         catch(NameNotFoundException e) {             Log.e("Not data shared", e.toString());         }    }} | 
"com.writer.data" in the highlighted line is the package name of the first application which we used to share data.
Following is the out put of second application :
 

 
 









