If an application has saved data using SharedPreference like this:
SharedPreferences.Editor preferencesEditor = getDefaultSharedPreferences.edit();
preferencesEditor.putInt("count", mValue);
preferencesEditor.apply();
can an other application have access to these data after reboot like this for example:
SharedPreferences sharedPref = getDefaultSharedPreferences();
int value = sharedPref.getInt("count",0);
And if it is not posssible, how is possible from an application to store variable so that other application have access to it?
Yes it is possible, but both apps need to be signed by the same certificates among other things, check this answer for details.
An application can also share and access data from a public content provider and this is usually the recommended way of sharing data among different applications.
You can also have a shared file in storage that both apps can read and write.
Related
In the official site of Android, for getPreference() method, it is said that :
Retrieve a SharedPreferences object for accessing preferences that are
private to this activity.
And here(How do I get the SharedPreferences from a PreferenceActivity in Android?) it is said that :
These preferences can only be used within the particular activity and
can not be used by other components of the application.
However, in url http://skillgun.com/android/basics/interview-questions-and-answers/paper/25 5th question,
it is said that it is not guaranteed to be protected as it will be
stored with the name of Activity.
I am confused whether other components of an application(such as activity,service etc.) can access the shared preference created by getPreference() method. is a shared preference created by calling getPreference() method accessible only for an activity for all circumstances?
Basically if you use shared preferences you will be able to read and write the preference from any part of your app. But other apps will not be able to access this information.
The statement regarding not being protected refers to the fact that rooted users (and apps) can read this files from the phone internal storage. So avoid at any cost saving sensitive user information in Shared Preferences. Ex. Dont store userNames, Passwords, personal details, etc.
Use shared preferences for simple things you wish to store Ex. If developing a contacts app you can store whether the user likes to read his contacts firstName LastName or LastName FirstName.
This kind of data is very short and not compromising.
If you require to store sensitive information always encrypt the data first.
I want to write a content provider for an app that exposes some data sored in the app's shared preferences. Is it possible to do that?
Why do you want to to do that ? Normally Preferences are secret data which should be only available to source app.
Incase you want to do that, you can read other's app preferences by obtaining the context of source application
Context otherAppsContext = createPackageContext("other.application.package", mode);
Also you should understand that you can’t get all preferences. When you will receive results you will see that PRIVATE and WORLD_WRITABLE aren’t available.
I've been searching for days to solve this, but with no success.
I want to get the shared preference settings from my old app and put it on to my new app.
but I've encounter some security issue (suspect).
my code:
Context c = createPackageContext("my.app.pkg", Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences sp = c.getSharedPreferences("my.app.pkg", Context.CONTEXT_IGNORE_SECURITY);
Running the code above giving me this:
Attempt to read preferences file /data/data/my.app.pkg/shared_prefs/my.app.pkg_preferences.xml without permission
even thought the object sp is not null, but it does not retrieve anything from my old app.
I tried googling around and seems like most people can run the code above with no errors.
Is there anything I've missed out?
We've done exactly this for our Android book Android in Practice. The key is to use the same process and user ID for both apps. The sample code and sample apps are on Google Code (SharedProcessApp1 and SharedProcessApp2). You can go from there.
You can indeed share preferences across applications. That's why it's called SharedPreferences.
What you need to do is make sure both application are signed with the same certificate and they both share the same SharedUserId in the AndroidManifest.xml file: read here.
This is because the SharedPreferences you get from
PreferenceManager.getDefaultSharedPreferences(context)
has always MODE_PRIVATE.
However in an application you can also get a SharedPreferences object within a Context with the following:
SharedPreferences prefs = getSharedPreferences("my_public_shared_prefs", MODE_WORLD_READABLE);
which you can freely retreive from another app with the following:
Context context = createPackageContext("my_target_app_package", Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences prefs = context.getSharedPreferences("my_public_shared_prefs", MODE_WORLD_READABLE);
Make sure you don't store any private information there because it's WORLD readable, which means you and anyone else can read that data.
To finish, if you want to retrieve the SharedPreferences of your old app you will need to update the old application with a SharedUserId in the Manifest file.
Android APplications runs in their own sandbox, so one application can not access data of other activity. BUt If an application want to share some of its data, this can be achieved by ContentProvider.
See Content Provider: http://developer.android.com/guide/topics/providers/content-providers.html
As per my understanding the values stored using the SharedPreferences in android can be used only by the app which created it.(correct me if wrong).
Then what is the significance of the name SharedPreferences.
Is something really shared here?
well it can be shared accross applications if you set the mode to WORLD READABLE or WORLD_WRITEABLE. (think ringtone settings, notification message etc...)
see this link
public static final int MODE_WORLD_READABLE
Since: API Level 1
File creation mode: allow all other applications to have read access to
the created file.
The SharedPreferences use a secure /data directory for preferences that are shared within your app's activities and services.
http://developer.android.com/reference/android/content/SharedPreferences.html
I basically want to be able to have multiple SharedPreferences files for my app. The names of these will be based off of a string entered by the user. Then when the user wants to restore, I want to create a popup that will allow them to pick from all available SharedPreferences files. Is there a way to see what SharedPreferences files are in the directory? Or is there a better way to store this?
TIA
You could save a csv list of SharedPreferences as a SharedPreference...
But I think if you are going to have multiple configurations, better approaches would be either a small SqliteDatabase or ".ini" files in the internal storage.
If you insisted on using SharedPreferences as the storage mechanism, what you could do is use the default shared preferences (get it with SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);) to store a list of the preference files created (using something like the putStringSet method at http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#putStringSet%28java.lang.String,%20java.util.Set%3Cjava.lang.String%3E%29). You can then retrieve them later and generate the popup list.