Saving a value on android - android

I want to save a String value on Android and have access to this
String every time the application starts.
For instance the String value will have the user's name which he has created by
his own. And after restaring the api he would have this name already on the top.
Like a cookie or sth. How to save such file on android memory ?
Can someone guide me?

Use the following:
private static final String PREFS_NAME = "YOUR_TAG";
private static final String DATA_TAG = "DATA_TAG";
private static final String data = "oh, what a data!";
//init
SharedPreferences mSettings = context.getSharedPreferences(PREFS_NAME, 0);
//commit
SharedPreferences.Editor editor = mSettings.edit();
editor.putString(DATA_TAG, data);
editor.commit();
//clear
SharedPreferences.Editor editor = mSettings.edit();
editor.clear();
editor.commit();
//get
String data= mSettings.getString(DATA_TAG, null);
If you would like to store your data not only on your local device but share this data across the devices for the same user or even across different platforms you can use a simple API of Skiller SDK. I am ofcourse from Skiller, for full disclosure.

If you're looking to only store the data locally, use SharedPreferences. If you're looking to have this data persist across multiple devices (always available for this user), check out Swarm's Cloud Data, which provides a simple set/get (like SharedPreferences) for storing data in the cloud.

This is what SharedPreferences is all about.

Best way to implement is, store in shredpreferences, read it on lauch of your application. Shared Preference API. HEre is an example
on how to use SharedPreferences

Related

save strings to string.xml, Android

I'm trying to create a way to get input from a user and save the string in the string.xml, so that when I launch my Application again, it will be there. Can I use the string.xml or do I have to save it another way?
Can I use the string.xml or do I have to save it another way?
You will have to do something different. The resources can't be changed after it has been compiled.
Have a look at Storage Options to see which way is best for you. The best way will be determined by things such as the type and amount of data you will be storing.
The link I posted sums it up pretty well then you can dig into the structures that you think might work best for your situation. From the link:
Shared Preferences
Store private primitive data in key-value pairs.
Internal Storage
Store private data on the device memory.
External Storage
Store public data on the shared external storage.
SQLite Databases
Store structured data in a private database.
Network Connection
Store data on the web with your own network server.
in order to save your strings you can use SharedPreferences
to save your data
SharedPreferences pos;
public String fileName = "file";
pos = getSharedPreferences(fileName, 0);
SharedPreferences.Editor editor = pos.edit();
editor.putString("pwd","your string");
editor.commit();
to get your data
pos = getSharedPreferences(fileName, 0);
String data = pos.getString("pwd", "");

Best way to store a single user in an Android app?

I'd like my app to allow a single user to create a profile that is accessed every time they run the app. The user's profile contains a String entry and five int entries. I've thought about using an SQLite database to store the info between sessions, but it seems like overkill since I'd only need one row in a single table to store the data. However, I'm not sure if it's possible to use SharedPreferences on a String type of data.
Can anyone provide some insight into this issue please? Thank you!
Yes you can use SharedPreferences.
To write
Editor editor = getSharedPreferences("file_name", MODE_PRIVATE).edit();
editor.clear();
editor.putString("pref_name", myStringValue);
editor.commit();
To read back
SharedPreferences preferences = getSharedPreferences("file_name", MODE_PRIVATE);
String myStringValue = preferences.getString("pref_name", "default_value");
Regards.

Write File without MODE_WORLD_READ/WRITE

Okay, I have come across many tutorials on creating and writing/reading a file. What I want my Android app is to create a small text file where it will read and write some settings so my user information can be saved. The problem is every tutorial I have come across uses the SDcard (which allows the user and any other app to read) or use MODE_WORLD_READABLE which makes any app read off of it.
I was wondering how I can create and read/write my file but private or atleast keep it on the internal storage of the phone.
Here's an easy example. To read a preference do this:
SharedPreferences preferences = getSharedPreferences("YourAppName", MODE_PRIVATE);
String thisString = preferences.getString("KeyForThisString", "default");
You can getInt, getLong, etc.
To store the preferences, use these lines:
SharedPreferences preferences = getSharedPreferences("YourAppName",MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("KeyForThisString", thisString);
editor.commit();
As before, you can putInt, putLong, etc.
The available methods for this API are here: http://developer.android.com/reference/android/content/SharedPreferences.html
dont know how to use it much and from what im seeing at least, its not much of a difference unless im wrong.
I still recommend to use SharedPreferences technique. It's explicitly easy and save a lot of time.
You can use it to save any primitive data: booleans, floats, ints, longs, and strings.
For example, you can do that each time when user invokes any changes to your Application because it's high performance technique. By this way even on crash all edited info will be stored.
Suppose you have an Activity.
Use this method where data is your String that you wanted to save to file.
private void saveInfo(String data){
String key = "myKey";
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString(key , data);
editor.commit();
}
Now, when you launch your application again onCreate method invoked and you can load your information back:
private void loadInfo(){
String key = "myKey";
SharedPreferences mPrefs = context.getSharedPreferences(LauncherUI.PREFS_NAME, 0);
String yourData = mPrefs.getString(key, ""); // "" means if no data found, replace it with empty string
}
Here is a link to 5 types to store data: Storage Options

How exactly to save data in App Data Storage of applicationcraft?

I am newbie to Mobile development, I am using http://motech.applicationcraft.com/ to get some app going. I read about App Data Storage and wanted to use it to save some data.
I have done something like below.
3 text box with storage in d/b value TRUE.
Now I want on Click of Save button this 3 details gets saved.
My guess I need to write some Javascript on click of Save button, not sure how/what?
Pritesh
Internal storage in AC is deprecated so using d/b value= true will not do anything.
Store data securely into your own external database using Server Side functions (see this link )
Use SharedPreference to store data in App.
Like..
public static final String PREFS_NAME = "MyPrefsFile";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("name" valueOfName);
editor.putString("name" valueOfUserId);
editor.putString("name" valueOfPassword);
editor.commit();

SharedPreferences are dissapearing after a few minutes.

I have a strange problem and I havent been able to find a solution to it.
My SharedPreferences are dissapearing after a few minutes I create a shared preference editor and then commit every change I make. I then get the SharedPreferences values to use using:
settings = getSharedPreferences("settings", 0);
if(settings.contains("accesstoken")){
// create a string that stores the accesstoken from our settings.
String str_access = settings.getString("accesstoken", null);
if(accesstoken_file_data != null){
str_access = accesstoken_file_data;
}
After a while the access token I stored along with all other data stored in SharedPreferences doesn't appear to exist... Thats if I leave my phone for a few minutes. I am developing with a HTC One X.
I am using this method to add values:
String accesstoken = "someVal";
SharedPreferences.Editor editor = settings.edit();
editor.putString("accesstoken", accesstoken);
editor.commit();
I have not found any viable solutions to this yet... if someone could help or direct me that would be great thanks...
SharedPreferences have an unfortunate tendency to wipe its data if the app crashes or is force stopped-
And all saving methods (with the exception of external storage) are wiped if the app data is cleared from settings
You could try to use a different saving method for storing the data. You have databases (SQLite), internal and external storage. The last two are file-based.

Categories

Resources