Thursday 3 October 2013

Data Sharing between Android Applications

// siddhu vydyabhushana // 6 comments
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.


?
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 :

Data Sharing between android application

6 comments:

  1. 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.

    ReplyDelete
  2. Very nice and helpful tips. thanks for sharing with us "Data Sharing between Android Applications"

    Android Services in Delhi

    ReplyDelete
  3. 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.
    Data science Course Training in Chennai | No.1 Data Science Training in Chennai
    RPA Course Training in Chennai | No.1 RPA Training in Chennai

    ReplyDelete