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
Asked
Active
Viewed 1,127 times
0

Totti
- 665
- 5
- 12
- 26
-
You can use Android shared preferences: http://developer.android.com/reference/android/content/SharedPreferences.html, another solution can be: http://stackoverflow.com/questions/708012/android-how-to-declare-global-variables – Nermeen Jul 04 '12 at 12:46
-
Look at this question http://stackoverflow.com/questions/11326443/passing-variables-from-one-class-to-another-wiithout-using-the-intent-get-extra/11326561#11326561 – user370305 Jul 04 '12 at 12:47
-
thank you , you help me , i will use it – Totti Jul 04 '12 at 12:48
1 Answers
1
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();

RvdK
- 19,580
- 4
- 64
- 107