android save global variable - android

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

Related

Pushing a SharedPreference File before installing in android

I need to set some configurations for my android application, like the server IP and Port, which the application should use to communicate. How can I add do it?
I thought of using sharedpreferences. How can I push a sharedpreference file with configurations, before even installing the app.
P.S. I don't want to hardcode the IP and PORT in the code base.
Any suggestions would be helpful.
Thanks
It's not possible. An alternate would be to ship the app with a json config file - put that in the assets folder
And read the contents of the file to prefill values you need.
try {
InputStream inputStream = mContext.getAssets().open(mContext.getPackageName() + ".json");
 
int size = inputStream.available();
byte[] buffer = new byte[size];
 
if (inputStream.read(buffer) != size) {
throw new IndexOutOfBoundsException();
}
inputStream.close();
 
JSONObject jsonObject = new JSONObject(new String(buffer, StandardCharsets.UTF_8));
server_ip = jsonObject.getString("server_ip");
server_port = jsonObject.getString("server_port");
} catch (Exception e) {
e.printStackTrace();
}
}
I would personally put that code in a class that extends Application, so it is called only once.
You CAN NOT push anything before installing. But you can store necessary data in SharedPreferences and retrieve whenever you want. To write/read String data to/from SharedPreferences;
private static final String SHARED_PREFS = "sharedPrefYourApp";
private static final String TEXT = "IP/PORT";
private static final String KEY = "IP_PORT_KEY";
public static void saveData(Context context) {
SharedPreferences sharedPreferences =
context.getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(KEY, TEXT);
editor.apply();
}
public static String loadData(Context context) {
SharedPreferences sharedPreferences =
context.getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
String text = sharedPreferences.getString(KEY, "");
return text;
}
Another approach would be using something like Firebase Remote Config and then providing the app with configurations dynamically. Think of this approach as the app connecting to a static place; downloads configurations securely and then reacts to those configurations and connects to the end services securely.
If you are trying to make this secure, I would recommend using the Android keychain instead of SharedPreferences - I barely use SharedPreferences (although that's a preference or it depends on security and use case needs).
This is now available in Android via "Managed Configurations"
https://developer.android.com/work/managed-configurations

Android Store Pin locally

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

Can't save String to SharedPreference?

I am using this to write some String to shared preference.
SharedPreferences urls = getSharedPreferences("imagesRemoteUrls", 0);
editorUrls = urls.edit();
editorUrls.putString("url3", imageUrl3);
And i try to pull the url out by.
imageUrl3 = urls.getString("url3","nothing");
I dont know why but it returns "nothing" each time. Even when i log that the url has been put in the preference successfully.
Don't forget:
editorUrls.commit();
after your editorUrls.putString() line!

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