首先,是ANDROID源代码如下:
public abstract SharedPreferences getSharedPreferences (String name,int mode)
Retrieve and hold the contents of the preferences file 'name',returning a SharedPreferences through which you can retrieve andmodify its values. Only one instance of the SharedPreferencesobject is returned to any callers for the same name, meaning theywill see each other's edits as soon as they are made.
Parameters
name | Desired preferences file. If a preferences file by this name doesnot exist, it will be created when you retrieve an editor(SharedPreferences.edit()) and then commit changes(Editor.commit()). |
---|---|
mode | Operating mode. Use 0 or MODE_PRIVATE MODE_WORLD_READABLE MODE_WORLD_WRITEABLE MODE_MULTI_PROCESS MODE_MULTI_PROCESS |
Returns
- Returns the single SharedPreferences instance that can be used toretrieve and modify the preference values.
SharedPreferences settings = getSharedPreferences(PREFS_NAME,0);通过名称,得到一个SharedPreferences,这个Preferences 是共享的,共享的范围据现在同一个Package中
这里面说所的Package和Java里面的那个Package不同,貌似这里面的Package是指在AndroidManifest.xml文件中:
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.roiding.sample.note"
android:versionCode="1"
android:versionName="1.0.0">
而于一个Activity中调用
详解:
很多时候我们开发的软件需要保存一些用户设置的信息,例如是否记住密码、显示的字体大小等等。如果是window软件通常我们会采用ini文件进行保存,如果是j2se应用,我们会采用properties属性文件进行保存。
在Android国度里,我们使用SharedPreferences是最合适不过的了,SharedPreferences是一个轻量级的存储类,特别适合用于保存软件配置参数。SharedPreferences其背后是用xml文件存放数据,文件存放在/data/data/packagename/shared_prefs目录下。
下面我写了一个SharedPreferences_Demo,需要的同志可以Download下来看看。欢迎交流学习。
首先,我写了一个工具类,用来进行SharedPreferences的相关操作:
- package
com.iteye.dengwho; -
- import
android.content.Context; - import
android.content.SharedPreferences; - import
android.content.SharedPreferences.Editor; -
- public
class SharedPreferencesUtil { -
private SharedPreferences sp; -
private Editor editor; -
private final static String SP_NAME = "mydata"; -
private final static int MODE = Context.MODE_WORLD_READABLE -
+ Context.MODE_WORLD_WRITEABLE; -
-
public SharedPreferencesUtil(Context context) { -
sp = context.getSharedPreferences(SP_NAME, MODE); -
editor = sp.edit(); -
} -
-
public boolean save(String key, String value) { -
editor.putString(key, value); -
// 亿万不要忘了加commit呐~~~!!!! -
return editor.commit(); -
} -
-
public String read(String key) { -
String str = null; -
str = sp.getString(key, null); -
return str; -
} - }
这里我用我的血泪屎来提醒大家在save的操作时1000万不要忘了commit()。
另外,因为SharedPreferences本身就是以XML保存文件的,所以我们不用给它命名时多加“.xml”后缀。
下面是Activity类:
- package
com.iteye.dengwho; -
- import
android.app.Activity; - import
android.os.Bundle; - import
android.view.View; - import
android.view.View.OnClickListener; - import
android.widget.Button; - import
android.widget.EditText; - import
android.widget.Toast; -
- public
class SharedPreferenced_DemoActivity extends Activity { -
private Button saveBtn; -
private Button readBtn; -
private EditText inputEv; -
private EditText showEv; -
private SharedPreferencesUtil util; -
-
-
public void init() { -
util = new SharedPreferencesUtil(this); -
saveBtn = (Button) findViewById(R.id.save_btn); -
readBtn = (Button) findViewById(R.id.read_btn); -
inputEv = (EditText) findViewById(R.id.input_et); -
showEv = (EditText) findViewById(R.id.showinfo_et); -
// 设置监听 -
setListener(); -
} -
-
-
public void setListener() { -
saveBtn.setOnClickListener(new OnClickListener() { -
public void onClick(View v) { -
boolean b = util.save("mykey", inputEv.getText().toString()); -
String msg; -
if (b) { -
msg = "保存成功"; -
} else { -
msg = "保存失败"; -
} -
Toast.makeText(SharedPreferenced_DemoActivity.this, msg, -
Toast.LENGTH_SHORT).show(); -
} -
}); -
-
readBtn.setOnClickListener(new OnClickListener() { -
public void onClick(View v) { -
System.out.println("read..."); -
String value = util.read("mykey"); -
showEv.setText(value); -
} -
}); -
} -
-
@Override -
public void onCreate(Bundle savedInstanceState) { -
super.onCreate(savedInstanceState); -
setContentView(R.layout.main); -
init(); -
} - }
运行界面:
保存成功后我们可以找到相应的XML文件:
我们把XML文件提取出来打开,就可以看到我们保存的Key和Value了,小
访问其他应用中的Preference
- SharedPreferences
sharedPreferences = getSharedPreferences("first_app_perferences", Context.MODE_PRIVATE); - String
name = sharedPreferences.getString("name", ""); //getString()第二个参数为缺省值,如果preference中不存在该key,将返回缺省值 - int
age = sharedPreferences.getInt("age", 1);
II:访问其他应用中的Preference(在SecondApp中访问FirstApp的数据),前提条件是:FirstApp的preference创建时指定了Context.MODE_WORLD_READABLE或者Context.MODE_WORLD_WRITEABLE权限。
如:在<packagename>为com.first.app的应用使用下面语句创建了preference("first_app_perferences")。
在SecondApp中要访问FirstApp应用中的preference,首先需要创建FirstApp应用的Context,然后通过Context 访问preference,访问preference时会在应用所在包下的shared_prefs目录找到preference :
- Context
firstAppContext = createPackageContext("com.first.app", Context.CONTEXT_IGNORE_SECURITY); - SharedPreferences
sharedPreferences = firstAppContext.getSharedPreferences("first_app_perferences", Context.MODE_WORLD_READABLE); - String
name = sharedPreferences.getString("name", ""); - int
age = sharedPreferences.getInt("age", 0);
如果不通过创建Context访问FirstApp应用的preference,可以以读取xml文件方式直接访问FirstApp应用的preference对应的xml文件,如:
File xmlFile = new File(“/data/data/<packagename>/shared_prefs/first_app_perferences.xml”);//<packagename>应替换成应用的包名: com.first.app