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.
Related
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();
I am developing an android application which has a Activity with two buttons apartments and banks. When user clicks on apartments it should parse data from xml file and assign to the listview. This xml file is saved in the assets folder and I want to create some shared preferences for this file. I also want to update the shared preferences if my device finds the Internet connectivity.
This is the structure of my XML file:
<?xml version="1.0" encoding="UTF-8"?>
<address_book>
<apartments>
<apartment>
<name>Villa Alvarado</name>
<street_address>6050 Montezuma Rd San Diego CA 92115</street_address>
<web_address>http://housing.sdsu.edu/housing/apartments.aspx</web_address>
<phone>(619) 594-2747</phone>
</apartment>
<apartment>
<name>Zuma Apartments</name>
<street_address>6237 Montezuma Rd San Diego CA 92115</street_address>
<web_address>http://liveatzuma.com/</web_address>
<phone>(619) 265-9862</phone>
</apartment>
<apartments>
<banks>
<bank>
<name>USE Credit Union</name>
<street_address>5500 Campanile Drive San Diego CA 92182</street_address>
<web_address>https://www.usecu.org/home/home</web_address>
<phone>(866) 873-4968</phone>
</bank>
<bank>
<name>Wells Fargo</name>
<street_address>4690 63rd St San Diego CA 92115</street_address>
<web_address>https://www.wellsfargo.com/</web_address>
<phone>(619) 583-9084</phone>
</bank>
</banks>
</address_book>
Why not creating such xml file programatically using SharedPreferences
I don't know why you are saving an xml file in the assets folder while installation of the application. I guess you are not aware of the correct usage of the shared preferences,Let me throw some light on the concepts.
1. Creating SharedPreferences xml SharedPreferences are xml files that are created programatically from your code.For creating such xml file you need to write following code
SharedPreferences sharedpreferences = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
above statement will create an xml file at /data/data/YOUR_PACKAGE_NAME/shared_prefs/MyPreferences.xml
2.Creating Parameter: Now when you call sharedpreferences.getString(Name, "Butterflow"); this statement will create a parameter in xml file MyPreferences named as Name with a default value of Butterflow.
3.Modifying the value: Later you can modify the value of the parameter name by writing following piece of code
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Name,"Akshay"); //new value of the parameter
editor.commit(); //will commit your changes
4.Retreiving the value: For retrieving value at later time you can write
String stringValue = sharedprefernces.getString(Name, ""); //Now the second parameter will be useless and dummy because there is no default value needed
So why not creating such xml file programatically,seems easy to manipulate,isn't it?See here too How to use SharedPreferences in Android to store, fetch and edit values
I didn't understand your use case at first.
You shouldn't use SharedPreferences for that. Use a database. SQLite should be perfect for this use case.
You would have the following logic:
Parse the XML and write data to your database
Find Internet connectivity
Retrieve your data from your database and update it.
Some links to start:
Content Providers in Android and SQLite usage in Android application
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.
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();
i am using a properties file to get urls to call web services in my app. Is there a way to edit that through some android activity.
The properties file is in src/resources folder. i am accessing that file using Resourcebundle.
You cant edit any file in application pkg file, however you can have following options to achieve this:
Have Preferences, if when you fetched preferences are null, load preferences with that property file, else let it be as it is.
Edit values in preferences.
If you still insist to use a text file only, copy property file to filesystem, and edit that file whatever you want to edit.