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
| package com.writer.data; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; public class DataWriterActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); dataWriter(); } public void dataWriter(){ 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
| package com.datareader; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle; import android.util.Log; import android.widget.TextView; public class DataReaderActivity extends Activity { String dataShared; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); dataRead(); TextView tv = (TextView)findViewById(R.id.textView1); tv.setText(dataShared); } public void dataRead(){ 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 :
There area unit times once individuals follow ancient procedure for composition cash on pressing basis. The time has gone after you have followed typical procedure for obtaining cash support. You can’t perpetually make preparations addressing the difficulties unpredictable in nature while not composition finances from external sources. Applying for 12 month loans becomes a lot of easier during this modern era wherever you don’t ought to take obligations of others. an internet format consumes couple of minutes of yours as gaining control the obstacles becomes easier in few clicks in order that ends up in the hassle-free condition of life.
ReplyDeleteBurberry Outlet
ReplyDeleteOakley Eyeglasses Michael Kors Outlet Coach Factory Outlet Coach Outlet Online Coach Purses Kate Spade Outlet Toms Shoes North Face Outlet Coach Outlet Gucci Belt North Face Jackets Oakley Sunglasses Toms Outlet North Face Outlet Nike Outlet Nike Hoodies Tory Burch Flats Marc Jacobs Handbags Jimmy Choo Shoes Jimmy Choos
Burberry Belt Tory Burch Boots Louis Vuitton Belt Ferragamo Belt Marc Jacobs Handbags Lululemon Outlet Christian Louboutin Shoes True Religion Outlet Tommy Hilfiger Outlet
Michael Kors Outlet Coach Outlet Red Bottoms Kevin Durant Shoes New Balance Outlet Adidas Outlet Coach Outlet Online Stephen Curry Jersey
thanks for sharing this article
ReplyDeleteBest PHP summer training
Best Big Data Hadoop summer training
Very nice and helpful tips. thanks for sharing with us "Data Sharing between Android Applications"
ReplyDeleteAndroid Services in Delhi
ookkay... i am getting some trouble in getting this package code
ReplyDeleteandroid Introduction
Andriod mobile app development
questions regarding android
android
I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.
ReplyDeleteData science Course Training in Chennai | No.1 Data Science Training in Chennai
RPA Course Training in Chennai | No.1 RPA Training in Chennai