Are SharedPreferences stored in databases? - android

Is this correct?
SharedPreferences are stored in databases?

No it is not correct. SharedPreferences are stored as XML files in your applications directory.

I am agreed with #Octavian answer (up voted), it is stored inside the file.
SharedPreferrences are stored in
databases?
As i have written answer, NO, its stores inside the file inside your project directory.
To view this file, go to DDMS Perspective, click on the File Explorer.
From File Explorer, data -> data -> your project -> shared_prefs.
Above, shared_prefs is the folder contains all the shared preferences you have declared and used.

Actually shared preferences store value into a variable .it is saved as like key-value pair
below is the code is used for store and retrieve values through shared preference values.
SharedPreferences prefs=getSharedPreferences("Key", 0);
Editor e= prefs.edit();
e.putString("Name", "AAA");
e.commit();
For retrieve the shared prefs value use below code
SharedPreferences prefs=getSharedPreferences("Key", 0);
String s= prefs.getString("Name", "");

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();

create Shared Preference for xml file and update file when device finds internet connectivity

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

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.

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.

Preferences don't show up in data/data nor can i access them with code

I'm trying to create a game. Now for some data i need to keep(music, effects, background on/of) i would like to use preferences.
I create new xml file named preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference android:key="music" android:title="Some title" android:defaultValue="true"/>
</PreferenceScreen>
Then i go to the location(with root privileges) and there is only lib folder in data/data/my.app.folder/. No shared_prefs folder.
When i try to access the preferences it is null. I tried with getSharedPreferences() and with PreferenceManager.getDefaultSharedPreferences(). What could be the reason?
Also i don't have activity for the preferences, I want to manage them manually.
How can I get over this problem?
To use shared preferences you will have to create it first. Like this:
SharedPreferences prefs = getApplicationContext().getSharedPreferences(
"music", Context.MODE_PRIVATE);
To create preferences in first time in it:
final String BACKGROUND_DEFAULT_VALUE = "on";
final String DEFAULT_TITILE = "";
final String TITILE_KEY = "title";
final String BACKGROUND_KEY = "background_key";
Editor editor= prefs.edit();
editor.putString(TITILE_KEY, DEFAULT_TITILE);
editor.putString(BACKGROUND_KEY, BACKGROUND_DEFAULT_VALUE);
editor.commit();
Than to edit/and save preferences in it:
Editor editor= prefs.edit();
editor.putString(TITILE_KEY, "new title");
editor.putString(BACKGROUND_KEY, "new background value i.e on/off");
editor.commit();
Than To read preferences:
String background_value = prefs.getString(BACKGROUND_KEY);
String title_value = prefs.getString(TITILE_KEY );
And you can see your shared preference file at location, it will be there after the creation.
/data/data/<packagename>/shared_prefs/music.xml
For more details and help, The android sdk's sample directory contains an example of retrieving and stroing shared preferences. Its located in the:
<android-sdk-home>/samples/android-<platformversion>/ApiDemos directory
Hope it helps.
There are two different types of xml files related to preferences.
The one you are showing is the xml resource file representing the preference hierarchy (screen representation).
The second one is the file containing the actual key value pairs.
Creating the first one doesn't create the second one. You'll actually need to display the file using a PreferenceActivity (or the Fragment equivalent or your own display method which - as I understand - you intend to do) or you'll have to create the preferences in code.

Categories

Resources