Cannot read SharedPreferences from another application.

Edition:

I have one application that writes to SharedPreferences, like this:

Context otherAppsContext = null; try { otherAppsContext = createPackageContext("AfroKeyboard.com.rob", Context.CONTEXT_IGNORE_SECURITY); } catch (NameNotFoundException e) { } SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences("PREFS_PRIVATE", Context.MODE_WORLD_READABLE); Editor prefsPrivateEditor = sharedPreferences.edit(); prefsPrivateEditor.putString("layout02", jString); prefsPrivateEditor.putString("layout02name", "Russian Layout"); prefsPrivateEditor.commit(); 

and another application that should read from them

  Context otherAppsContext = null; try { otherAppsContext = createPackageContext("AfroKeyboard.com.rob", Context.CONTEXT_IGNORE_SECURITY); } catch (NameNotFoundException e) { } SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences("PREFS_PRIVATE", Context.MODE_WORLD_READABLE); Log.e( "name2" , "name2: "+sharedPreferences.getString("layout02name", "") ); 

but it returns empty.

What do you think, maybe the problem?

Thanks!

+2
source share
4 answers

You need to specify the application context. For instance:

 Context otherAppsContext = null; try { otherAppsContext = createPackageContext("<YOUR_PACKAGE_NAME>", Context.MODE_WORLD_READABLE); } catch (NameNotFoundException e) { } SharedPreferences prefs = otherAppsContext.getSharedPreferences("PREFS_FILE", Context.MODE_WORLD_READABLE); String result = prefs.getString("PREFERENCE_TAG_NAME", "DEFAULT_VALUE"); 

This way you can read the contents of the general preference "PREFERENCE_TAG_NAME", which is saved in the file

 /data/data/YOUR_PACKAGE_NAME/shared_prefs/<PREFS_FILE>.xml 

You can do this in different applications that will work, but for this, "YOUR_PACKAGE_NAME" should be the same.

If you want to change the value in any application, you will need to change the getSharedPreferences mode from:

 Context.MODE_WORLD_READABLE 

in

 Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE" 
+7
source

Since you are accessing SharedPreferences from another package, you need to use Context.createPackageContext().getSharedPreferences() instead of this.getSharedPreferences()

+2
source

Try the following:

Write SharedPreferences as:

  Context otherAppsContext = null; try { otherAppsContext = createPackageContext("AfroKeyboard.com.rob", Context.CONTEXT_IGNORE_SECURITY); } catch (NameNotFoundException e) { } myPrefs = otherAppsContext.getSharedPreferences("NAME_OF_SHARED_PREFERENCES", Context.MODE_WORLD_READABLE); Editor prefsEditor = myPrefs.edit(); prefsEditor.putString("layout02", jString); prefsEditor.putString("layout02name", "Russian Layout"); prefsEditor.commit(); 

Read SharedPreferences as:

 myPrefs = otherAppsContext.getSharedPreferences("NAME_OF_SHARED_PREFERENCES", Context.MODE_WORLD_READABLE); String s = myPrefs.getString("layout02name", "") ); 
+1
source

While this is the case, I have managed to complete this work.

  String packageName ="com.yourpackage.yourname" Context otherAppsContext = null; try { otherAppsContext = createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY); } catch (Exception e) { Toast.makeText(getApplicationContext(), "will crash", 3).show(); } SharedPreferences prefsother = otherAppsContext.getSharedPreferences(keyPackageName, Context.MODE_WORLD_READABLE); String key = prefsother.getString("key", "defValue");//use prefs normally here 
0
source

All Articles