【Androidノート9】Android Sharedpreferencesユーザーの好みのストレージを実現


アプリケーションの開発中に、ユーザーの好みやユーザー名のパスワードなどを記録する必要がある場合があります.これはデータのストレージに関連し、androidプラットフォームの下でデータを格納する方法は主に以下のいくつかの方法があります.
  
Shared Preferences
Store private primitive data in key-value pairs. 軽量なキー値ペアで格納
Internal Storage
Store private data on the device memory. デバイス上のファイルストレージ
External Storage
Store public data on the shared external storage. 外部のファイルストレージとは、一般的にSDカードに格納されているファイルのことで、プログラムのアンインストールに伴って削除しない利点があります
SQLite Databases
Store structured data in a private database. これは一般的なデータベースです
Network Connection
Store data on the web with your own network server. ネットワーク取得
では、今日は主に最初のShareepreferenceを共有します.これはandroidが提供する軽量レベルのデータストレージフレームワークです.形式はキー値のペアの形式です.xmlに詳しい友达は、このスタイルに慣れているはずです.「属性名-属性値」の形式です.名前から分かるように、このフレームワークは主にユーザーの好みを設定するために使用され、sessionにまたがる共有が可能であり、複数のactivityの間で操作でき、特別な権限を設定すれば、他のアプリケーションにもアクセスできるのが特徴です.以下に、使用する基本的な方法を示します.
1:取得タイプSharedPreferences主に2つの方法で入手できます
       getSharedPreferences()は2つのパラメータを受け取り、1つ目はファイルの表示であり、2つ目は権限であり、複数のプロファイルを作成するためのシーンである.getPreferences()-ファイル権限パラメータのみで、前とは異なり1つのファイルのみが作成されます.
今日は最初の方法を詳しくご紹介します~
まずは公式APIからの紹介
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 and modify its values. Only one instance of the SharedPreferences object is returned to any callers for the same name, meaning they will see each other's edits as soon as they are made.
説明:この方法はコンテキストから、nameというプロファイルを取り戻し、維持するために使用されます.SharedPreferencesオブジェクトを返します.このオブジェクトでキー値を編集して取得できます.各時点でSharedPreferencesdインスタンスが呼び出されるのは1つだけです.効果は、各呼び出し元が他の呼び出し元編集の効果をすぐに見ることができることです.
Parameters
name
 Desired preferences file. If a preferences file by this name does not exist, it will be created when you retrieve an editor (SharedPreferences.edit()) and then commit changes (Editor.commit()). 説明:必要なファイル名は、存在しない場合、editorを呼び出すと(好みのファイルを編集するために使用され、以下で説明します)自動的に作成されます.
mode
Operating mode. Use 0 or  MODE_PRIVATE  for the default operation,  MODE_WORLD_READABLE  and  MODE_WORLD_WRITEABLE  to control permissions. The bit  MODE_MULTI_PROCESS  can also be used if multiple processes are mutating the same SharedPreferences file. MODE_MULTI_PROCESS  is always on in apps targetting Gingerbread (Android 2.3) and below, and off by default in later versions. 操作モードは全部で4種類あります.
Returns
Returns the single SharedPreferences instance that can be used to retrieve and modify the preference values.
See Also MODE_PRIVATE          , ID MODE_WORLD_READABLE    MODE_WORLD_WRITEABLE    MODE_MULTI_PROCESS        ,2.3 ,
SharedPreferencesオブジェクトがあれば、お気に入りの編集と設定を行うにはEditorが必要です.
Editorは主にいくつかのputXXXの方法を含んで、支持します
booleans, floats, ints, longs, and strings.いくつかのタイプがあり、最後に追加が完了したら、必ずcommitメソッドまたはapplyメソッドを呼び出して修正後のコミットを行います.
valueを手に入れたいなら自分でSharedPreferenceクラスで対応するgetxxxメソッドを使えばいい.
もう言わないで、自分が臨時に書いた非常に簡単なコードを奉納して、みんなと分かち合います.
     
package com.yui;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.util.Log;
/**
 *   android SharedPreference   demo
 * @author Octobershiner
 * @version 1.0 2011/11/4
 * */
public class PreferActivity extends PreferenceActivity {
    /** Called when the activity is first created. */
	
	//      
	private static final String MY_SETTING = "mySetting";
	private static final String COLOR = "color";
	private static final String DEFAULT_COLOR = "blue";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //get the setting files 
        SharedPreferences myPreference = this.getSharedPreferences(MY_SETTING, Context.MODE_PRIVATE);
        //edit the file
        Editor editor = myPreference.edit();
        editor.putString(COLOR, "red");
        
        /**
         *         , myPreference    COLOR       
         *   DEFAULT_COLOR   temp
         *            putString   ,log       
         * */
        String temp = myPreference.getString(COLOR, DEFAULT_COLOR);
        //  Log            
        Log.i("TAG","now i prefer "+temp);
    }
}

最后に、私は1つの问题を闻きたいです.Oさんはandroid 2.3のSDKソースコードを见ましたが、SharedPreferenceはただのインタフェースで、私はその実现を见つけていません.ネット上にも相応の解答がありません.知っている友达がすぐに私に连络してほしいです.交流してください.ありがとうございます.11时です.実験室には人がいません.帰って寝るつもりです.