1,使用SharedPrefrences
用于簡單少量的數據,數據的格式簡單:都是普通的字符串,標量類型的值等,比如各種配置信息等等
**SharedPrefrences與Editor簡介**:
創建SharedPreferences實例,通過Context.getSharedPreferences(String name,int mode);方法來獲取SharedPreferences的實例
mode的值:
*Context.MODE_PRIVATE;該SharedPreferences數據只能被本應用程序調讀,寫
* Context.MODE_WORLD_READABLE;該SharedPreferences數據能被其他程序讀,但是不能寫
* Context.MODE_WORLD_WRITEABLE;該SharedPreferences數據能被其他程序讀,寫
?SharedPreferences保存的數據主要是類似于配置信息格式的數據,因此他保存的數據主要是簡單類型的key-value對
* SharedPreferences接口主要負責讀取應用程序的Preferences數據,提供如下常用的方法訪問key-value對
* boolean contains(String key);判斷是否包含key的數據
* abstract Map getAll();獲取全部鍵值對
* boolean getXxx(String key,xxx,defValue);獲取指定的key對應的value值,如果key不存在,返回默認defvalue,xxx可以是Boolean,float,int,long,String等各種基本類型的值
SharedPreferences接口本身并沒有提供寫入數據的能力,而是通過 SharedPreferences的內部接口Editor寫入數據,SharedPreferences調用edit()方法即可獲得它所對應的Editor對象
Editor提供了如下方法:
* SharedPreferences.Editor clear();清空所有數據
* SharedPreferences.Editor putXxx(String key,xxx value);存入指定key對應的數據,xxx可以是Boolean,float,int,long,String等各種基本類型的值
* SharedPreferences.Editor remove(String key);刪除指定key的數據
* Boolean commit();當Editor編輯完成之后,調用該方法提交修改
例子:一個按鈕寫數據,一個按鈕讀數據
activity_main.xml
~~~
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Write_SharedPreference" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Read_SharedPreference" />
</LinearLayout>
~~~

MainActivity.java
~~~
package com.hust.sharedpreferences;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
/*
* 創建SharedPreferences實例,通過Context.getSharedPreferences(String name,int mode);方法來獲取SharedPreferences的實例
* mode的值:
* Context.MODE_PRIVATE;該SharedPreferences數據只能被本應用程序調讀,寫
* Context.MODE_WORLD_READABLE;該SharedPreferences數據能被其他程序讀,但是不能寫
* Context.MODE_WORLD_WRITEABLE;該SharedPreferences數據能被其他程序讀,寫
*
*
* SharedPreferences保存的數據主要是類似于配置信息格式的數據,因此他保存的數據主要是簡單類型的key-value對
*
* SharedPreferences接口主要負責讀取應用程序的Preferences數據,提供如下常用的方法訪問key-value對
* boolean contains(String key);判斷是否包含key的數據
* abstract Map<String,?> getAll();獲取全部鍵值對
* boolean getXxx(String key,xxx,defValue);獲取指定的key對應的value值,如果key不存在,返回默認defvalue,xxx可以是Boolean,float,int,long,String等各種基本類型的值
*
* SharedPreferences接口本身并沒有提供寫入數據的能力,而是通過 SharedPreferences的內部接口Editor寫入數據,SharedPreferences調用edit()方法即可互毆它所對應的Editor對象
* Editor提供了如下方法:
* SharedPreferences.Editor clear();清空所有數據
* SharedPreferences.Editor putXxx(String key,xxx value);存入指定key對應的數據,xxx可以是Boolean,float,int,long,String等各種基本類型的值
* SharedPreferences.Editor remove(String key);刪除指定key的數據
* Boolean commit();當Editor編輯完成之后,調用該方法提交修改
*
* */
public class MainActivity extends Activity {
//
SharedPreferences preferences;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//實例化SharedPreferences對象,讀數據
preferences=getSharedPreferences("test",Context.MODE_WORLD_READABLE);
//實例化Editor對象,寫數據
editor=preferences.edit();
Button read=(Button) findViewById(R.id.button2);
Button write=(Button) findViewById(R.id.button1);
read.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String time=preferences.getString("time", null);
int rnd=preferences.getInt("rnd", 0);
String result=time==null?"您暫時還未寫入數據":"寫入時間:"+time+"\n上次生成的數據數是:"+rnd;
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
}
});
write.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日"+"hh:mm:ss");
editor.putString("time", sdf.format(new Date()));
editor.putInt("rnd", (int)(Math.random()*1000));
editor.commit();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
~~~
SharedPrefrences文件的存儲位置:

test.xml

待續。。。
- 前言
- Eclipse搭建android環境及Genymotion模擬器安裝問題解決方法
- 表格布局(TableLayout)及重要屬性
- 幀布局(FrameLayout)及屬性
- layout_width和width,layout_height和height
- UI組件之TextView及其子類
- UI組件之TextView及其子類(一)TextView和EditText
- UI組件之TextView及其子類(二)RadioButton和CheckBox
- UI組件之TextView及其子類(三)ToggleButton和Switch
- UI組件之TextView及其子類(四)AnalogClock,DigitalClock
- UI組件之TextView及其子類(五)計時器Chronometer
- UI組件之ImageView及其子類(一)ImageView顯示圖片
- UI組件之ImageView及其子類(二)ImageButton ,ZoomButton
- UI組件之AdapterView及其子類關系,Adapter接口及其實現類關系
- UI組件之AdapterView及其子類(一)三種Adapter適配器填充ListView
- UI組件之AdapterView及其子類(二)GridView網格視圖的使用
- UI組件之AdapterView及其子類(三)Spinner控件詳解
- UI組件之AdapterView及其子類(四)Gallery畫廊控件使用
- UI組件之AdapterView及其子類(五)ListView組件和ListActivity
- UI組件之AdapterView及其子類(六)ExpandableListView組件和ExpandableListActivity的使用
- UI組件之 ProgressBar及其子類(一)ProgressBar進度條的使用
- UI組件之ProgressBar及其子類(二)SeekBar拖動條和RatingBar星級評分條的使用
- ViewFlipper的功能和用法
- Toast的功能和用法
- TabHost選項卡的 功能和用法
- AlertDialog創建6種對話框的用法
- Android基于監聽的事件處理機制
- Android基于回調的事件處理
- Handler消息傳遞機制(一)
- Handler消息傳遞機制(二)Handler,Loop,Message,MessageQueue的工作原理
- 啟動Activity的兩種方式startActivity和startActivityForResult(一)
- 啟動Activity的兩種方式startActivity和startActivityForResult(二)
- Activity的生命周期理解
- Bundle在Activity之間交換數據
- 通過 Intent 傳遞類對象
- Intent對象詳解(一)
- Intent對象詳解(二)
- 使用指定的Action,Category調用系統Activity
- 使用Action,Data屬性啟動系統Activity
- Android數據存儲的三種方式-SharedPrefrences,File,SQLite