1

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?

Ehsan
  • 2,676
  • 6
  • 29
  • 56
  • You should use `FileProvider` or `ContentProvider`. `SharedPreferences` of yours app dont have r/w permissions to others – Stanislav Bondar Jul 12 '17 at 10:59
  • Possible duplicate of [Data sharing between two applications](https://stackoverflow.com/questions/5745243/data-sharing-between-two-applications) – Suraj Rao Jul 12 '17 at 12:29

4 Answers4

2

You can use content provider . here is detail and example

enter image description here

Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75
1

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

MuM6oJuM6o
  • 107
  • 10
0

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

humazed
  • 74,687
  • 32
  • 99
  • 138
0

Try changing to world readable:

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

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