hi
i have using shared preference for saving user name and password ,but when my application crashes i lost my data,i need to re login again(Only some crashes i lost the data ),how can i solve this problem ?
SharedData.userInfo = PreferenceManager
.getDefaultSharedPreferences(ctx);
SharedData.userAdd = SharedData.userInfo.edit();
SharedData.userAdd.putString("userEmailAddress", uname);
SharedData.userAdd.putString("userPassword", upassword);
SharedData.userAdd.commit();
I use it like this:
SharedPreferences sharedPref = getSharedPreferences("MyData",MODE_PRIVATE);
SharedPreferences.Editor prefEdit = sharedPref.edit();
prefEdit.putString("VariableName","Value");
prefEdit.commit();
and it works for me always.
Related
I have an app that receives push messages and stores a timestamp in a SharedPreference. When the App is quit i get a NPE caused by this:
SharedPreferences pref = MainActivity.mainActivity.getSharedPreferences("LAST_SYNC", Context.MODE_PRIVATE);
It works fine when the app is running at the front or even in multitasking. I'd appreciate some advice on how to handle processes when the app isn't running. Thanks
Code:
// Store Push received time.
Log.d("eLOQ", "Updating last time synced");
DateFormat lastSyncFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm");
SharedPreferences pref = MainActivity.mainActivity.getSharedPreferences("LAST_SYNC", Context.MODE_PRIVATE);
SharedPreferences.Editor ed = pref.edit();
ed.putString("LAST_SYNC_TIMESTAMP", lastSyncFormat.format(new Date()));
ed.apply();
For some functionality, I want to store PIN which is the user input in settings screen. I want to store that pin locally in mobile until app exists in that specific device.
I have used SharedPreferences to achieve this. Does this the good approach? or any other better approach?
SharedPreferences settings = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("1234", YOUR_PIN);
// Apply the edits!
editor.apply();
// Get from the SharedPreferences
SharedPreferences settings = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
int PIN = settings.getInt("YOUR_PIN", 0);
Following is the code I used to save SharedPreference:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putString("deliveryId", obj.get("deliveryId").toString());
editor.commit();
This is not working in some devices, but working in some devices. It is working on KitKat but doesn't work on JellyBean and Lollipop.
Following is the code I used to get the data:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String deliveryId = preferences.getString("deliveryId", "0");
Always gives the default value, i.e. 0, on some devices.
I think default preference creating problem for you try like below for saving SharedPreferences
SharedPreferences preferences = getSharedPreferences("<Pref Name>", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("deliveryId", obj.get("deliveryId").toString());
editor.commit();
to get the data:
SharedPreferences preferences = getSharedPreferences("<Pref Name>", MODE_PRIVATE);
String deliveryId = preferences.getString("deliveryId", "0");
Happy Coding!
I am trying to save Shared Preferences using this
SharedPreferences sharedPref = getSharedPreferences("BluefreeSharedPreferences",0);
PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
prefEditor = sharedPref .edit();
prefEditor.putString("UserName", userName);
prefEditor.putString("Password", password);
prefEditor.commit();
On Samsung Galaxy 1900 with 2.2.1 its not saving shared Preferences but on Emulator with 2.2.1 and HTC Y one with 4.0.3 OS it working fine.
I read some post as here http://code.google.com/p/android/issues/detail?id=14359 anyone can guide me is it still issue?
i don't think you've initialized the editor right, here's a little code i've written a while ago, it makes a preference of a counter and increments it upon execution
SharedPreferences counter = getSharedPreferences("prefs", Context.MODE_PRIVATE);
SharedPreferences.Editor count_editor = counter.edit();
int currentCount = counter.getInt(packageName +"", 0);
currentCount++;
count_editor.putInt("times clicked", currentCount);
count_editor.commit();
try this
http://developer.android.com/guide/topics/data/data-storage.html#pref
I am retrieving about 7 URL's from a service. I want to be able to write these URL's some where and have my application read from them in another activity.
The thing that makes this tricky is the URL's change every week. So i would need to overwrite the current URL's. I don't want to make the url's stack up on top of each other where they never overrite and by the end of a month there are 24 unused URL's.
What and how is the best way to do this?
Use SharedPreferences!
lol here is a sample:
public void SetUrls(String url[]) {
SharedPreferences settings = getSharedPreferences("myPrefs", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("url0", url[0]);
editor.putString("url1", url[1]);
editor.putString("url2", url[2]);
editor.putString("url3", url[3]);
editor.putString("url4", url[4]);
editor.putString("url5", url[5]);
editor.putString("url6", url[6]);
editor.commit();
}
public String[] getUrls() {
SharedPreferences settings = getSharedPreferences(DEALSPOTR_PREFS, 0);
String url[] = new String[7];
url[0] = settings.getString("url0", "default");
url[1] = settings.getString("url1", "default");
url[2] = settings.getString("url2", "default");
url[3] = settings.getString("url3", "default");
url[4] = settings.getString("url4", "default");
url[5] = settings.getString("url5", "default");
url[6] = settings.getString("url6", "default");
return url;
}
it really depends on your client implementation, you can store them in sqlite, or in local cache or shared preferences. Android allows apps to have a small cache.
As you store your urls you can timestamp them and check if they have expired and stuff.
Refere to the docs on android Data Storage facilities.