SharedPreferences是Android平台上一个轻量级的储存类,用来保存应用的一些常用配置,比如Activity状态,Activity暂停是,将次activity的状态保存到sharedPereferfernces中;当Activity重载,系统回调方法onSaveInstanceState时,再从SharedPreferences中将值取出。
SharedPreferences提供了java常规的Long、Int、String等类型数据的保存接口。
提示最终是以xml方式来保存,整体效率来看不是特别的高,对于常规的轻量级而言比SQLite要好不少,如果真的存储量不大可以考虑自己定义文件格式。xml处理时Dalvik会通过自带底层的本地XML Parser解析,比如XMLpull方式,这样对于内存资源占用比较好。
SharedPreferences数据的四种操作模式:
Context.MODE_PRIVATE
Context.MODE_APPEND
Context.MODE_WORLD_READABLE
Context.MODE_WORLD_WRITEABLE
Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件.
Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件。
添加:
//获取sp对象
SharedPreferences sharedPreferences = getSharedPreferences("wpc",MODE_PRIVATE);
//获取编辑
SharedPreferences.Editor edit = sharedPreferences.edit();
//数据处理
edit.putString("name","wpc");
edit.putInt("age",21);
edit.putFloat("money",25.3f);
//提交数据
edit.commit();
查找:
SharedPreferences sharedPreferences = getSharedPreferences("wpc",MODE_PRIVATE);
String name = sharedPreferences.getString("name", "无法访问数据");
int age = sharedPreferences.getInt("age", 0);
float money = sharedPreferences.getFloat("money", 1.2f);
Log.i("---user",name+age+money);
修改:
SharedPreferences sharedPreferences = getSharedPreferences("wpc", MODE_PRIVATE);
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putString("name","蔡徐坤");
edit.commit();
删除:
SharedPreferences sharedPreferences = getSharedPreferences("wpc",MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove("name");
editor.commit();
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File path = Environment.getExternalStorageDirectory();
File file = new File(path,"2012.txt");
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write("你好自己".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
File path = Environment.getExternalStorageDirectory();
File file = new File(path,"2012.txt");
try {
FileInputStream fileInputStream = new FileInputStream(file);
int len = 0;
byte[] bytes = new byte[1024];
StringBuilder stringBuilder = new StringBuilder();
while ((len=fileInputStream.read(bytes))!=-1)
{
String str = new String(bytes,0,len);
stringBuilder.append(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public class MyAsyncTask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
String path = strings[0];
try {
URL url = new URL(path);
HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setConnectTimeout(2000);
httpURLConnection.setReadTimeout(2000);
httpURLConnection.connect();
if (httpURLConnection.getResponseCode()==200){
InputStream inputStream = httpURLConnection.getInputStream();
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
int len = 0;
byte[] bytes = new byte[1024];
File path1 = Environment.getExternalStorageDirectory();
File file = new File(path1,"wpc.jpg");
FileOutputStream fileOutputStream = new FileOutputStream(file);
while ((len=inputStream.read(bytes))!=-1){
fileOutputStream.write(bytes,0,len);
}
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}