Android Default Shared Preferences File Readable By Multiple Apps? - android

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.

Related

Store a string in a shared place, accessible to a set of applications only

Imagine these applications:
com.example
com.example.app1
com.example.app2
One solution is to use android:sharedUserId='com.example' in both app1 and app2 and use the same certificate to sign all packages, so that all of them can access the shared preferences of the com.example.
However, this has a drawback. app1 and app2 can share something only and if only com.example is installed first.
Is there a way around this, so that applications app1 to appN can access a shared place, only limited to them, without the need for a central useless package to be used only as a shared Linux username?
Instead of using shared preferences you can write data in a file :
https://developer.android.com/reference/java/io/File
and that file can be stored in an internal storage which can be accessible by all the 3 applications.

Application settings/properties in Xamarin Forms similar to Shared Preferences in Android

I wish to store settings/properties at the Application level as well as Page wise. In other words, I need different sets of settings which will stay even when the app is restarted. I have considered Application.Current.Properties and Settings Plugin for Xamarin Forms but they don't seem to provide the same functionality as Shared Preferences in Android, where you can simply pass context and get Preferences for that context. Any help is appreciated.
Thanks
You can use the settings plugin for Xamarin Forms if you data you want to save does not need securing.
You could create a settings object, serialise and store with a context key if you need to store different preferences per context
https://github.com/jamesmontemagno/SettingsPlugin
If it is usernames, passwords or tokens and needs securing one option is the secure storage plugin
https://github.com/sameerkapps/SecureStorage
You might consider a local SQLite database to store application settings that persist through app and phone restarts.
https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/databases/
You could use Xamarin.Auth. It uses Keychain for iOS and KeyStore for Android to persist data.
"Saved Accounts are uniquely identified using a key composed of the account's Username property and a "Service ID". The "Service ID" is any string that is used when fetching accounts from the store."
https://github.com/xamarin/Xamarin.Auth
Section 4.2 from README file

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.

How to create sharedpref shared by few apps?

I have 4-5 different apps and I want to keep their settings parameters common. I want to do it through sharedpref.
Please suggest how to share sharedpref among different apps without making it public for all app.
using following function:
getSharedPreference(String name, int mode)
retrieves and holds content of file path with particular name.
MODE_PRIVATE: File creation mode: the default mode, where the created
file can only be accessed by the calling application (or all
applications sharing the same user ID).
MODE_WORLD_READABLE: File creation mode: allow all other applications
to have read access to the created file.
MODE_WORLD_WRITEABLE : File creation mode: allow all other
applications to have write access to the created file.
To access World Readable/Writeable from different app:
Context myContext = createPackageContext("com.example.app",
Context.MODE_WORLD_READABLE); // where com.example.app is the app containing the preferences
SharedPreferences testPrefs = myContext.getSharedPreferences
("test_prefs", Context.MODE_WORLD_READABLE); //test_prefs is the name in getSharePreference's argument
You could create a service that will be used by those applications, it will be managing your preferences. For service you can specify which applications can use it.
Use same key to read and write shared preference in all your apps you will get the same result as you get in app1 and in app2 ...

Why is a special file used in Android to store installation id instead of SharedPreferences?

I am trying to figure out the best way to store my application key and other application specific data. Android uses a file to do it - http://android-developers.blogspot.com/2011/03/identifying-app-installations.html
I found that using SharedPreferences is faster and more efficient. Why does Android use a dedicated file just to store the installation ID instead of using preferences? Both require the application context, so that is not a reason.
I guess so that the UUID is persisted across installations if the phone is not factory-reset.
An applications SharedPreferences are removed when that application is uninstalled. So if a user uninstalled/re-installed and the ID was stored in SharedPreferences then the ID would be different after the re-installation.

Categories

Resources