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!
Related
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);
On Android you can use a SharedPreferences class to store small quantity of data. Example:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();
What is the class equivalent to SharedPreferences in Ionic 2? How can it be used?
Ionic 2 has a storage module. The docs are here. It doesn't use SharedPreferences under the hood, but will:
"attempt to use IndexedDB, WebSQL, and localstorage, in that order."
Here is an example from the site I linked:
import { Storage } from '#ionic/storage';
export class MyApp {
constructor(storage: Storage) {
// set a key/value
storage.set('name', 'Max');
// Or to get a key/value pair
storage.get('name').then((val) => {
console.log('Your name is', val);
})
}
}
i want to save a variable to be global variable on all screens i have, for example, for every connect to the localhost i am using 10.0.2.2 so i have to write that ip on every connect,and when i want to try my application on my mobile device i have to go to code and replace all the 10.0.2.2 with my system static ip 192.168.1.101 ,is there any way to say that ip on a global variable? i read that i have to used string on android but i don't know how, any help please
Use SharedPreferences to store and retrieve it: http://developer.android.com/reference/android/content/SharedPreferences.html
Example: http://developer.android.com/guide/topics/data/data-storage.html#pref
public static final String PREFS_NAME = "MyPrefsFile";
//retrieve
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String ip= settings.getString("ip");
//use ip
//store
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("ip", mIp);
editor.commit();
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
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.