Android Store Pin locally - android

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

Related

SharedPreferences not saving in some Android versions

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!

android save global variable

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

getting Exception on Shared Preferences Store

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

How to retrieve String's and write them to a file or resource and overrwrite and reuse it?

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.

Android shared prefrance

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.

Categories

Resources