Android: Retrieving shared preferences of other application
Assuming the preference are WORLD_READABLE, this might work:
final ArrayList<HashMap<String,String>> LIST = new ArrayList<HashMap<String,String>>();
// where com.example is the owning app containing the preferences
Context myContext = createPackageContext("com.example", Context.MODE_WORLD_WRITEABLE);
SharedPreferences testPrefs = myContext.getSharedPreferences("test_prefs", Context.MODE_WORLD_READABLE);
Map<String, ?> items = testPrefs .getAll();
for(String s : items.keySet()) {
// do something like String value = items.get(s).toString());
}
Get access to shared preferences of another application
What Waqas says is correct, you can't use another applications data/shared preference unless it's shared somehow. But if you have access to both applications you should not use:
PreferenceManager.getDefaultSharedPreferences();
As the default uses MODE.PRIVATE which means you can only read the shared preference from the app it's initialized in.
If you have access to both applications you could use either:
PreferenceManager.setDefaultValues();
In order to change the default mode or use a preference with a name and mode and instead of getting the default use:
getSharedPreferences(NAME, MODE);
You can read more in the docs and maybe figure out if it suits your needs.
Use Shared Preferences of other application
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 :)
Retrieving sharedpreferences in another activity
One of the nice things about the SharedPreference
class is that it is designed to be available to any Activity
within you application. You can retrieve any SharedPreference
with two simple calls:
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
String desiredPreference = sharedPreferences.getString("NameOfSharedPreference", "DefaultValue");
While it may seem obvious make sure that you have actually created and stored the data you wish to save before you try and retrieve it. To continue on the example above, use the following code snippet to store your data into SharedPreferences
:
Editor editor = sharedPreferences.edit();
editor.putString("NameOfSharedPreference", "ValueToStore");
editor.commit();
Related Topics
Cursorloader Usage Without Contentprovider
Android Create Shortcuts on the Home Screen
Set Icon For Android Application
How to Align Views at the Bottom of the Screen
Android: How to Bind Spinner to Custom Object List
Manifest Merger Failed:Uses-Sdk:Minsdkversion 14
How to Crop Circular Area from Bitmap in Android
How to Display Toast in Android
Singleton Object Becomes Null After App Is Resumed
How to Resume Existing Fragment from Backstack
Offline Speech Recognition in Android (Jellybean)
What Android Tools and Methods Work Best to Find Memory/Resource Leaks
How to Install Trusted Ca Certificate on Android Device
Disabling the Fullscreen Editing View for Soft Keyboard Input in Landscape