Write File without MODE_WORLD_READ/WRITE - android

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

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", "");

Does SharedPreferences work in Eclipse emulator?

I am attempting to save a user id using SharedPreferences. Do values saved as SharePreferences persist across all Activities in my application so I can access the userid from any application? Below is my code for saving the userid.
userid = result.substring(3, result.length());
Log.d("userid at onpostexecute", userid);
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit(); // to update userid
editor.putString("userid", userid);
editor.commit();
And here is the code for accessing the userid from the SharedPreferences in another activity.
SharedPreferences prefs = getPreferences(MODE_PRIVATE); // to access userid
String userid = prefs.getString("userid", "");
Log.d("shared prefs userid", userid);
What is strange is that the above code is in my onCreate method but it doesn't show up in the Logcat even though other log data is displayed before and after this code. So is there something wrong with my code that I can't even get it to display in my logcat? I can't even tell if it is being updated.
Values saved as sharedPreferences can be shared between activities if you tell it to. Right now you are creating a preference that is only accessible to that same activity. You need to use:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
Like this you are creating a sharedPreference between your application. There is a lot of explanation on the topic in the accepted answer to another question. Link to question
As discussed in the answer the way you are using to save the preference will only work in the same activity that saved it.
I assume you mean can you access them from any Activity, yes, they do persist even when you leave your app and come back. From the Docs
The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).
If this Log.d("userid at onpostexecute", userid); doesn't even show up then I would put a breakpoint there and make sure you have a value for userid. I would also check your logcat filters to make sure that you are getting all logs. Just make sure that the type in the spinner is set to "verbose" just to be sure

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.

Saving a value on 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

Categories

Resources