-1

I found the same questions on stack and used this way

   public void setProgramAllProgress(int all){
        SharedPreferences prefs = CApplication.getAppContext().getSharedPreferences(BASE_PATH,
                Context.MODE_WORLD_READABLE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt("all", all);
        editor.commit();
    }


    public int getProgramAllProgress(String packageName){
       Context con=null;
        try {
            con = CApplication.getAppContext().createPackageContext(packageName, 0);
            SharedPreferences pref = con.getSharedPreferences(
                    BASE_PATH, Context.MODE_PRIVATE);
            int data = pref.getInt("all", -1);
            return data;
        } catch (PackageManager.NameNotFoundException e) {
            Log.e("Not data shared", e.toString());
        }
        return -1;
    }

But it doesn't work. I get the context from package, but SharefPrefs are always empty.

Jenya Kirmiza
  • 511
  • 8
  • 21
  • 1
    I don't think you can access SharedPreferences of other applications. – iTurki Aug 26 '15 at 12:18
  • @iturki you can, but only if the application has set them as WORLD_READABLE, which is deprecated. So no one should use this, but it is possible. – F43nd1r Aug 26 '15 at 12:20
  • @F43nd1r if it's possible why it's not working. If it's deprecated what should i use instead? – Jenya Kirmiza Aug 26 '15 at 12:21
  • @JenyaKirmiza for one-way data sharing use Content Providers. For two-way communication use one of the possible IPC methods (google it). – F43nd1r Aug 26 '15 at 12:23

2 Answers2

1

referring to this question i think you have to do the following:

In Application1:

SharedPreferences prefs = getSharedPreferences("demopref",
                    Context.MODE_WORLD_READABLE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("demostring", strShareValue);
            editor.commit();

and, in Application 2:

try {
    con = createPackageContext("com.sharedpref1", 0);
    SharedPreferences pref = con.getSharedPreferences(
    "demopref", Context.MODE_PRIVATE);
    String data = pref.getString("demostring", "No Value");
    displaySharedValue.setText(data);
} catch (NameNotFoundException e) {
    Log.e("Not data shared", e.toString());
}

Tell me if it works!:)

EDIT: anyway, Content Providers are the best way to do it

EDIT 2: if you strongly want to use sharedPrefs, try adding in manifest this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hello"
android:versionCode="1"
android:versionName="1.0" 
android:sharedUserId="com.example"> //<-- this line

EDIT 3: here is a guide on how to use ContentProvider :)

Community
  • 1
  • 1
Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66
1

That would require the application which´s SP you want to access to use MODE_WORLD_READABLE when writing data to its SP. This constant is deprecated and Google strongly recommends not to use it:

Creating world-readable files is very dangerous, and likely to cause security holes in applications. It is strongly discouraged; instead, applications should use more formal mechanism for interactions such as ContentProvider, BroadcastReceiver, and Service.

So if the app you want to access is not developed by your own I assume that its SPs are private and if it´s your own app than you should find a different way for sharing data.

sschmid
  • 1,341
  • 1
  • 16
  • 22
  • 1
    what way? ContentProvider? or create some file on storage? – Jenya Kirmiza Aug 26 '15 at 12:33
  • I never happend to implement a ContentProvider (so honestly I do not know all their features) but I assume that you should go with them, since they are the recommended way and were built for exactly that purpose. SharedPreferences are also just XML-files in your app´s data-directory. – sschmid Aug 26 '15 at 12:40
  • anyway thanks for help. I'll try to do it the right way – Jenya Kirmiza Aug 26 '15 at 12:41