Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android數據持久化三板斧:SharedPreferences、文件與SQLite详解

編輯:關於 Android 開發

Android數據持久化三板斧:SharedPreferences、文件與SQLite详解

本文開篇先聚焦其中最輕量的一種——SharedPreferences。

一、使用SharedPreferences

它適用於簡單、少量的數據,格式也單純:無非是字符串、標量類型的值等,比如各類配置信息。

SharedPreferences與Editor簡介:

創建SharedPreferences實例,可通過Context.getSharedPreferences(String name, int mode)方法獲取。
mode的取值:
* Context.MODE_PRIVATE:該數據只能被本應用讀寫;
* Context.MODE_WORLD_READABLE:該數據能被其他程序讀,但不能寫;
* Context.MODE_WORLD_WRITEABLE:該數據能被其他程序讀寫。

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接口本身並不提供寫入能力,寫入要通過其內部接口Editor完成——調用edit()即可獲得對應的Editor對象。Editor提供的方法有:
* SharedPreferences.Editor clear():清空所有數據;
* SharedPreferences.Editor putXxx(String key, xxx value):存入指定key的數據;
* SharedPreferences.Editor remove(String key):刪除指定key的數據;
* boolean commit():編輯完成後調用該方法提交修改。

例子:一個按鈕寫數據,一個按鈕讀數據。

activity_main.xml

  

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

SharedPreferences文件的存儲位置:


test.xml

待續……

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved