Ehsan
Ehsan

Reputation: 2781

How to share data Between two Application

I am trying to share some data between two applications with SharePreferences. (If i am goning wrong way please tell me the correct way to send data between diffrent applications). my code is like this: In Application1 (Sender):

 SharedPreferences prefs = getSharedPreferences("odenevisha.com.apps.test_01",
      Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString("DATA", "123");
    editor.apply();

In Application2 (Receiver):

   try {
      Context con = createPackageContext("codenevisha.com.apps.test_01", 0);//first app package name is "codenevisha.com.apps.test_01"
      SharedPreferences pref = con.getSharedPreferences(
        "demopref", Context.MODE_PRIVATE);
      String your_data = pref.getString("DATA", "No Value");
      Log.e("LOGO", "TEXT IS: " + your_data);

    }
    catch (PackageManager.NameNotFoundException e) {
      Log.e("LOGO", e.toString());
    }

and in the manifest of both Applications, I defined this below line into Tag:

   android:sharedUserId="any string"
   android:sharedUserLabel="@string/label"

but it does not work! what is the problem?

Upvotes: 1

Views: 3879

Answers (4)

MuM6oJuM6o
MuM6oJuM6o

Reputation: 107

You could also use the AIDL to share content between two applications in Android. https://developer.android.com/guide/components/aidl.html

Upvotes: 1

paursand
paursand

Reputation: 1

Try changing to world readable:

SharedPreferences prefs = getSharedPreferences("odenevisha.com.apps.test_01",
  Context.MODE_WORLD_READABLE);

https://stackoverflow.com/a/11949750/7890950

Upvotes: 0

Sanket Kachhela
Sanket Kachhela

Reputation: 10856

You can use content provider . here is detail and example

enter image description here

Upvotes: 2

humazed
humazed

Reputation: 76942

you should instead use ContentProviders

Content providers can help an application manage access to data stored by itself, stored by other apps, and provide a way to share data with other apps. They encapsulate the data and provide mechanisms for defining data security. Content providers are the standard interface that connects data in one process with code running in another process. Implementing a content provider has many advantages. Most importantly you can configure a content provider to allow other applications to securely access and modify your app data

if you want to use SharePreferences you must sign the apps with the same key too

Upvotes: 0

Related Questions