標籤:android 資料存放區 sharedpreferences
接下來四篇我們來介紹Android中用於資料存放區的四種方式:
- SharedPreferences
- Sqlite
- Files
- 網路
今天我們先來看一個最簡單的:SharedPreferences.
這種資料存放區方式是最簡單,最輕便,也最實用的,但是只能用來儲存基礎資料型別 (Elementary Data Type)。我們來看看怎麼使用:
1. res/ layout/ activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <Button android:id="@+id/btn_putValue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="添加value"/> <Button android:id="@+id/btn_get" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="擷取當前SP值"/> <Button android:id="@+id/btn_remove" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="刪除key的值"/> <Button android:id="@+id/btn_clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清除所有資料"/></LinearLayout>
2. MainActivity.java
package com.example.ch6_01_sharepreferences;import java.util.Map;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.content.SharedPreferences;import android.util.Log;import android.view.Menu;import android.view.View;import android.widget.Button;public class MainActivity extends Activity { private static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn_getSP = (Button) findViewById(R.id.btn_get); btn_getSP.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences sp = getPreferences(MODE_PRIVATE); Map<String, Object> map = (Map<String, Object>) sp.getAll(); Log.e(TAG, ""+map); String playmusic = sp.getString("playmusic", ""); Log.e(TAG,"playmusic:"+playmusic); } }); Button btn_putValue = (Button) findViewById(R.id.btn_putValue); btn_putValue.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences sp = getPreferences(MODE_PRIVATE); SharedPreferences.Editor et = sp.edit(); et.putBoolean("autologin", true); et.putString("playmusic", "true"); et.commit(); } }); Button btn_remove = (Button) findViewById(R.id.btn_remove); btn_remove.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences sp = getPreferences(MODE_PRIVATE); SharedPreferences.Editor et = sp.edit(); et.remove("autologin"); et.commit(); } }); Button btn_clear = (Button) findViewById(R.id.btn_clear); btn_clear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences sp = getPreferences(MODE_PRIVATE); SharedPreferences.Editor et = sp.edit(); et.clear(); et.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; }}
先來簡單介紹一下SharedPreferences, 它是android封裝起來,專門儲存喜好設定的,底層是xml檔案,起存放的目錄在:data/ data/ 應用程式套件名/ shared_prefs/ 目錄下,當然這寫檔案處理都不需要你來做,都是android自己來完成,你所要走的就是得到SharedPreferences對象,然後增,刪,改,查資料,然後commit() 就行了,這裡為什麼要強調commit, 新手使用的時候很容易忘記,當然commit() 也包含有事務的思想,畢竟是檔案操作,會耗費時間,所以用一個事務來做,萬無一失。
增加資料:(修改資料當然也用這個,只是填入不同的值就行了)
<pre name="code" class="java">SharedPreferences sp = getPreferences(MODE_PRIVATE);SharedPreferences.Editor et = sp.edit();et.putBoolean("autologin", true);et.putString("playmusic", "true");et.commit();
刪除資料: (刪除指定key的資料)
SharedPreferences sp = getPreferences(MODE_PRIVATE);SharedPreferences.Editor et = sp.edit();et.remove("autologin");et.commit();
清除資料:(清除xml檔案中的所有資料)
SharedPreferences sp = getPreferences(MODE_PRIVATE);SharedPreferences.Editor et = sp.edit();et.clear();et.commit();
擷取xml中所有資料:(使用getAll()方法可以獲得xml檔案中的所有資料,匯出的類型是一個map,可見xml檔案中是以鍵值對的方式在儲存資料)
<pre name="code" class="java">SharedPreferences sp = getPreferences(MODE_PRIVATE);Map<String, Object> map = (Map<String, Object>) sp.getAll();Log.e(TAG, ""+map);String playmusic = sp.getString("playmusic", "");Log.e(TAG,"playmusic:"+playmusic);
獲得某個key的value,可以用getString(key, 預設值)。如果未找到key,則返回預設值。
擴充:
好了,看完了怎麼用,我們在來看看SharedPreferences產生的檔案,我們要注意,這裡獲得SharedPreferences 使用的方法是直接調用 activity.getPreferences( MODE_PRIVATE), 我們並沒有指定 xml檔案的名字,那麼它會以MainActivity作為檔案名稱建立xml檔案,有圖有真相:
當然我們在得到SharedPreferences的時候也可以指定xml檔案的名稱:
另外一種獲得SharedPreferences的方式:
SharedPreferences spUtil = getSharedPreferences("Util", MODE_PRIVATE);SharedPreferences.Editor etUtil = spUtil.edit();etUtil.putFloat("temp", 10.2f);etUtil.commit();就系統會在目錄下建立 Util.xml 檔案:
最後我們來看 MODE_PRIVATE 這個參數,話說是私人的意思,我本以為私人的意思是:一個Activity建立的xml不能被其他Activity共用,其實不然,是指其他application,其他的應用不能訪問。在同一個application中,只要擷取SharedPreferences的名字是對的,那麼都是從 shared_prfs/ 目錄下去找檔案,而且所有activity都可以互連。
最後讓我們來看看xml檔案中產生的到底是什麼,我把MainActivity.xml 匯出來了,看看裡面的內容:
好了,Over
Android入門筆記 - 資料存放區 - SharedPreferences