Android shared preferences names - android

I can obtain SharedPreferences via:
getSharedPreferences("someName", ...)
How do I get a list of all names of shared preferences in my application?
If it's not possible via code, is there any way to do it via adb (without root)?

You can get all the preferences.xml from /data/data/(package)/shared_prefs/(package)_preferences.xml

Looks like there's no API for this, the only way is to list files in shared_prefs directory. Something like:
File sharedPrefsDir = new File(getFilesDir(), "../shared_prefs");
File[] files = sharedPrefsDir.listFiles();

Related

Whether the user can edit the preferences file?

Is there a way that user with root device could modify shared preferences file ? For example with Text editor .
My app store encrypted string and some counter inside this file and this values shouldn't be edited !
If this files could be edit with editor, is there anywhere else that i could store this values ?
And for more detail preferences mode is set to MODE_PRIVATE if it will help.
Short version
1.Is there a way that preferences file could be edited ?
2.If yes where could this values be saved instead .
You might also try encrypting your Shared Preferences.
https://prashantsolanki3.github.io/Secure-Pref-Manager/
This is a very easy to use library which automatically encrypts and decrypts your shared preferences.
SecurePrefManager.with(this)
.set("user_name")
.value("LoremIpsum")
.go();

Android Preference file set name

I am currently following this tutorial Android Settings
my problem is it save the app settings as
com.packagename_preferences.xml
Is there any way I can set its file name? like this
mychoosennamehere_preferences.xml
or
somenamesiwanttouse.xml
Although you can rename the preference file but you should not do that. Actually, the exact location and name of this preference file is not documented, so I'd suggest you don't rely on some conventions when trying to access this file directly, since the location and name may be changed in future - SharedPreferences should be the only way to access this file.And your App will stop working.
Anyhow here is the way to rename your preference file
String fileName="mychoosennamehere_preferences";
File f=new File("/data/data/eywa.musicplayer/shared_prefs/"+"com.packagename_preferences"+".xml");
f.renameTo(new File("/data/data/eywa.musicplayer/shared_prefs/"+fileName+".xml"));
SharedPreferences mySharedPreferences=getSharedPreferences(fileName,Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.remove(PlayListName);
editor.putString(fileName, fileName);
editor.commit();
PlayListName=fileName;
You can change the name of the .xml file (right-click -> rename).
Then change the name in your code that points to the file.

Getting /data/data/my.app/files path programmatically?

In my app, I've just successfully downloaded a file and I opened it like this:
OutputStream output = openFileOutput("myfile.txt", Context.MODE_PRIVATE);
With a little browsing, I've found my file in /data/data/my.app.namespace/files/myfile.txt. I don't want to hardcode the path but later on in my app I'm going to want to access myfile.txt and I want to dynamically get that folder path. I've tried most of the folders in the Environment object like Environment.getDataDirectory().getAbsolutePath() but that only returns /data. Where can I look to get where private files go? Or is it safe to assume that this structure won't change? I don't like hardcoding it, but it would work...
you could do it by using context.getFilesDir() + "/filename.ext"

Best way to store multiple users's data in android application

I'm quite new to android and I'm trying to build an application where userss can create multiple settings profile and store them in the internal storage. Sinse the data being stored are settings, I was thincking to store them in xml file and creating a different xml file for each profile.
In example:
profile1.xml
profile2.xml
etc...
Is it a good idea?
How can I create those xml files in the internal memory and write each setting in them?
I've seen something like
String FILENAME = "profile1";
String setting1 = "set";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(setting1.getBytes());
fos.close();
but how can I specify that the file created is in xml format? And how can I create the setting corresponding tags(to parse them later by id for example)?
Thank you and sorry for my bad english.
Alessandro
You can use shared preferences, its format is xml, its quite easy to access it, modify/read and should be quite straightforward to implement.
Here is an example that might help you http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/
Here are storage options explained http://developer.android.com/guide/topics/data/data-storage.html

how to modify sharedpreference name .I want to have specific name for preferences file

Default the name of the preferences file saved on the device is always be _preferences. I want to modify it to "mypreference" on to the device how can I i do that.
SharedPreferences are stored in an xml file in the app data folder, i.e.
/data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PREFS_NAME.xml
or the default preferences at:
/data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PACKAGE_NAME_preferences.xml
SharedPreferences added during runtime are not stored in the Eclipse project.
Taken from here.
For the custom name you'll want, take a look at this thread here. For example:
String fileName="mypreference";
File f=new File("/data/data/eywa.musicplayer/shared_prefs/"+whatever+".xml");
f.renameTo(new File("/data/data/eywa.musicplayer/shared_prefs/"+fileName+".xml"));
SharedPreferences mySharedPreferences = getSharedPreferences("list_of_playlist",Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.remove(PlayListName);
editor.putString(fileName, fileName);
editor.commit();
PlayListName=fileName;
Get a new preference file (getSharedPreferences) with the name "mypreference", and copy everything from "_preferences" to it.

Categories

Resources