How to use one time domain name in android? - android

I have a RESTful web-service, I am retrieving a data to android device
So here My ip address/Doamin name may change like..
192.168.0.1 or1-255 etc or It may be www.stackexchange.com or www.stackoverflow.com
Like my data will be stored in 192.168.0.1/rst/api/login or www.stackoverflow.com/rest/api/sitemview
So to over come this I want to use domain name as One time when i install application
in my application I have other pages like Login,display list-view, Single ItemView.
So this domain name should stored in device and pass to other activities every time when I use.
I used shared preferences like
public static void savePreferences(Context ctx, String key, String value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(ctx);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
so when they load
public static Object loadSavedPreferences(Context ctx, String key) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(ctx);
return sharedPreferences.getString(key, "");
}
So here My problem is that This one time value is not permanent when ever I force close or Restart devise that passing value is not working
Any suggetion

If your domain name fixed, use below code.
public class WS{
public static final String domain="http://blah.ws";
}
Access it in each activity
with
String apiPath=WS.domain+your_rest_path;

Related

Remember/Forget device function on android app

I am building an app which connects & then controls a device.
I am wanting to add a remember device function and forget device function so the user can stick to one device or switch.
I am new to android so any help on how to implement this would be great.
You can keep the information in SharedPreferences. If you are connecting to a Bluetooth device then you need to remember the Mac address of the device.
Store and retrieve the info using the following functions.
public static void addDataToSharedPrefsString(String key, String value,Context context) {
SharedPreferences.Editor sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(
context).edit();
sharedPreferences.putString(key, value);
sharedPreferences.commit();
}
public static String getDataFromSharedPrefString(String key,Context context) {
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(context);
String value = preferences.getString
(key, "");
return value;
}
Add to shared pref by
addDataToSharedPrefsString("macAddress", "MAC123q4e",context);
get the shared value by
getDataFromSharedPrefString("macAddress",context);
if you want to delete an existing item in shared pref, set the value null.
addDataToSharedPrefsString("key", null,context);
I hope this is your requirement and this solution would help you.

How to change password after user login in android ? In shared Preferences?

I use token based authentication in my app. When user logins through Android app the server returns token which needs to be sent with each subsequent request.
I need to store that value on the devices. Since token is a simple string, I thought I'd use SharedPreferences to hold that value.
Two think confuse me on which method save token in sharedpref. and other one is where to receive the token while implementing change password.
Use Aynch task For network task
Use Post Method Api
try something like this.. Create a class for saving values
public class SharedPreferenceCustom {
private String defValue = "";
private SharedPreferences sharedPreferences;
public SharedPreferenceCustom(Context context) {
sharedPreferences = context.getSharedPreferences("app_name", Context.MODE_PRIVATE);
}
public void setSharedPref(String inputKey, String inputValue) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(inputKey, String.valueOf(inputValue));
editor.apply();
}
public String getSharedPref(String inputKey) {
return sharedPreferences.getString(inputKey, defValue);
}
}
and call whenever needed
Call by
SharedPreferenceCustom sp = new SharedPreferenceCustom(mContext);
sp.setSharedPref("KEY", "VALUE");
// or
sp.getSharedPref("KEY");

Shared Preference fetching previous URL in android

I'm trying to save my URL using Shared Preference and the URL keeps changing for every second. Now my problem is when I try to fetch the URL with the Shared Preference variable, it is fetching previously saved URL and not the current URL and also when I run my app for first time it is showing blank but second time it is showing the URL that should have come first time.
This is my code.
SharedPreferences preferences = getSharedPreferences("temp", getApplicationContext().MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();
editor.putString("name", url);
editor.commit();
And to retrive the URL I wrote it in this way.
SharedPreferences preferences=getSharedPreferences("temp", getApplicationContext().MODE_PRIVATE);
String name=preferences.getString("name","not loaded");
What should I do ?
MODE_MULTI_PROCESS was there to refresh data from disk every time. Because multiple processes could be modifying preferences data and data loaded in memory for one process wouldn't have changes from other process. It has been deprecated for not working reliably.
As far as your particular problem, I'd recommend something like:
public class PrefStore {
private static final String PREF_NAME = "app_prefs";
private final SharedPreferences mPreferences;
public static final String KEY_URL = "key_url";
public PrefStore(Context context) {
mPreferences = context.getSharedPreferences(PREF_NAME,Context.MODE_PRIVATE);
}
public void saveUrl(String url){
mPreferences.edit().putString(KEY_URL,url).apply();
Log.i("PREF_TEST",String.format("Write URL:%s at:%s by thread:%s",url, new Date(), Thread.currentThread().getId()));
}
public String getUrl(){
String url = mPreferences.getString(KEY_URL,null);
Log.i("PREF_TEST",String.format("READ URL:%s at:%s by thread:%s",url, new Date(), Thread.currentThread().getId()));
return url;
}
}
Log will help you inspect whether its a timing/thread problem.

Android: Unbale to save boolean in SharedPreferences

Hey guys I am unable to save a boolean value using SharedPreferences. The value is ALWAYS true for some reasons. Here is how I save the value:
public static void setSharedPreference(Context ctx, String keyValue, boolean value){
SharedPreferences sp = ctx.getSharedPreferences(Constants._preferencesName, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean(keyValue,value);
editor.commit();
}
And this is how I get it back:
public static boolean getBooleanPreference(Context ctx, String keyValue){
boolean prefValue;
SharedPreferences sp = ctx.getSharedPreferences(Constants._preferencesName, ctx.MODE_PRIVATE);
prefValue = sp.getBoolean(keyValue, false);
return prefValue;
}
What is wrong?!
Your code is syntactically correct, but I suspect you are passing different Context while saving than you are passing while reading from prefs. This will result in accessing different shared preferences storage. This is especially easy to step on if you are doing your writes and reads in different activities and decide to pass this as context. Unless there's a reason for doing so then you most likely want to reach your preferences from anywhere in your app then use always application context instead (getApplicationContext()).
Everything is correct in your code.
The ONLY possibility of a mistake is when you are calling these methods. Please use getApplicationContext() while putting and retrieving data.
And please do a "Clear data" for the app and start with a clean SharedPreference.

How to use Android SharedPreferences

I am working on an Android project where a user can store some data but is able to delete and change/alter/update the values, I have been searching for a while now for a tutorial, but are not able to find any, so I was wondering, Is it possible to use SharedPreferences for that?
While you can always read the docs and know more about SharedPreferences, for a quick start here are few static methods from one of my project which you can use.
public static boolean getBooleanPrefs(Context ctx, String key) {
return PreferenceManager.getDefaultSharedPreferences(ctx).getBoolean(key, false);
}
public static void setBooleanPrefs(Context ctx, String key, Boolean value) {
PreferenceManager.getDefaultSharedPreferences(ctx).edit().putBoolean(key, value).commit();
}
public static String getStringPrefs(Context ctx, String key) {
return PreferenceManager.getDefaultSharedPreferences(ctx).getString(key, "");
}
public static void setStringPrefs(Context ctx, String key, String value) {
PreferenceManager.getDefaultSharedPreferences(ctx).edit().putString(key, value).commit();
}
public static int getIntPrefs(Context ctx, String key) {
return PreferenceManager.getDefaultSharedPreferences(ctx).getInt(key, 0);
}
public static void setIntPrefs(Context ctx, String key, int value) {
PreferenceManager.getDefaultSharedPreferences(ctx).edit().putInt(key, value).commit();
}
public static void clearPrefs(Context ctx) {
PreferenceManager.getDefaultSharedPreferences(ctx).edit().clear().commit();
}
So far if data is limited to some values, yes you can use SharedPreferences for that. You can easily update/alter/clear values in SharedPreferences.For usage of Shared preferences, refer these
Android Shared preferences example
http://developer.android.com/guide/topics/data/data-storage.html#pref
http://www.tutorialspoint.com/android/android_shared_preferences.htm
But if your data is not limited and have some repititive type of values to be stored.For eg. data of app users, their info and all that, then you should go for local database using SQLite. For SQLite,you should go through this tutorial
For pros and cons of SQLite and SharedPreferences, you should go through this answer
shared preference is good way to store values.
need to declare shared preference and stored value to it like.
SharedPreferences prefrs = PreferenceManager
.getDefaultSharedPreferences(getApplication());
Editor editor = prefrs.edit();
editor.putString("key",abc);
editor.commit();
you can easily get that value like below...
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
String name = prefs.getString("key", "default");
you can delete that stored value and can use for stored new value like below
SharedPreferences prefrs = PreferenceManager
.getDefaultSharedPreferences(getApplication());
SharedPreferences.Editor editor = prefrs.edit();
editor.clear();
editor.commit();
finish();

Categories

Resources