android access default shared preferences from different packages - android

I have an Android application with more source packages. In the base package I have a PreferenceActivity used to configure the preferences of the app. From all the activities within the same package as the PreferenceActivity, I can access these preferences by using PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
However, from another package, activities use another shared preferences file when I call PreferenceManager.getDefaultSharedPreferences(getApplicationContext());. This stops me from accessing the preferences from the PreferenceActivity.
How can I solve this? I want to retrieve the same shared preferences in every package in the application.

Can't you just use a name for the shared preference and then get the same name for all the applications? Look under shared preferences at: http://developer.android.com/guide/topics/data/data-storage.html#pref
Edit: I think what you look after is here, no reason to take out parts as it gives a pretty good explanation: How do I get the SharedPreferences from a PreferenceActivity in Android?

Related

Does SharedPreferences name need to be unique?

Probably a dumb question, but I"m using a SharedPreference with a few different names "MyPrefs1", "MyPrefs2" etc.
I'm assuming this is restricted to my app. i.e. if some other app tries to use the same name, it won't overwrite my values
I pretty much believe my understanding is correct, but the name "SharedPreferences" seem to indicate that it can be shared between apps? (is it for Sharing between activities?)
That's correct, the SharedPreferences are stored in your app's private folder (to be exact, in /data/data/your package name/shared_prefs).
You can give them whatever name you want.
SharedPreferences represent the preferences which can be shared between different components of your application. The SharedPreferences you create in your application is never exposed with other applications.
Whether you use PreferenceManager.getDefaultSharedPreferences() or Context.getSharedPreferences("file_name", Context.MODE_PRIVATE); both are particular to your application only.
Note - SharedPreferences or Preferences is not exposed to other applications.
Although you assumption is right that the SharedPreferences are not shared between apps and thus names cannot clash, it's a good practice (besides of put a specific name to your shared preferences file) to add the package of your application as a prefix. This is useful if you have different flavours and you don't want to overwrite your preferences among them, especially when you're testing an app and don't want to screw up the original values of the shared preferences files.

SharedPreferences: What are they shared with?

I find the name of stored preferences on Android (SharedPreferences) to be a tad confusing. It was probably chosen with a purpose, so how exactly are these preferences shared? e.g. I don't want other applications accessing my app's information willy-nilly.
What is the difference between preferences from getPreferences() and preferences from getSharedPreferences(), and what is the difference between the different modes? How should I choose which to use? (Heck, if I have a multi-activity program, will using just the regular getPreferences be detrimental?)
My imagination regarding the use of these things is still pretty limited.
It was probably chosen with a purpose, so how exactly are these preferences shared?
They are shared among all components of your application (e.g., all of your activities).
e.g. I don't want other applications accessing my app's information willy-nilly.
SharedPreferences are private to your application by default.
What is the difference between preferences from getPreferences() and preferences from getSharedPreferences(), and what is the difference between the different modes? How should I choose which to use? (Heck, if I have a multi-activity program, will using just the regular getPreferences be detrimental?)
I am not quite certain what you are referring to, since you provided just bare method names with no classes. If you are intending on collecting preference values from the user via the preference screen system, use PreferenceManager.getDefaultSharedPreferences(). I generally use that for everything.
Sharedprefernce are basically key,value pairs with can are saved in an xml and is accessible by an application. You can use it to save some setting value or default values or any other form of key value pairs.
So a key,value pair can be saved in one activity can be accessed in other activity.
Different modes means that whether the data saved can be used by other application or not. You should not worry about all modes. As MODE_WORLD_READABLE & MODE_WORLD_WRITEABLE are deprecated now.
For the difference:-
Difference between getShared/get preference

Android: Cannot access other app's SharedPreference

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

Android: one time only variable storing

I have an appliaction which will show a welcome window only the first time the user starts the app. What would be the best way to store this boolean variable (i.e. "isFirstTime") to phone storage? Should I use Shared Preferences or Internal Storage?
The docs say that if I use Internal Storage my "preference" file will automatically get removed upon uninstallation which is quite handy.
I want a clean, simple and fast solution.
Yes, I'd recommend using Shared Preferences. Basically you could put a shared preference with a key of "isFirstTime" and a type of boolean set to false. Then in your main activity do something like:
getBoolean (isFirstTime, true);
This, if it can't find isFirstTime will give you true, allowing you to do an if-statement based on the result.
I agree that SharedPreferences would probably be the most "clean, simple, and fast solution" that you are looking for. SharedPreferences are also deleted when the application is uninstalled.
Are the shared preferences associated with the App deleted when the app is removed?
SharedPreferences are your best option for this.

Android Default Shared Preferences File Readable By Multiple Apps?

How can I have a shared preferences file that can be read by more than one app. In particular by an admin app and the app it is administrating?
Have you tried getSharedPreferences() with MODE_WORLD_WRITEABLE (assuming that both apps are supposed to be able to write to it)? One of the apps will have to create a context using the package of the other app.

Categories

Resources