AndroidのIO - SharedPreferenceとEditor
SharedPreferences
主に少量データのやり取りに使用する。
設定のON、OFFとか。
Editor
SharedPreferencesは書き込み機能はないので、edit()と通して対応するEditorオブジェクトを
取得して書き込みを行う。
SharedPreferencesの一部メソッド
- boolean constains(String key)
- abstract Map<String, ?> getAll()
- boolean getXxx(String key, xxx defValue)
Editorの一部メソッド
- SharedPreferences.Editor clear()
- SharedPreferences.Editor putXxx(String key, xxx value)
- SharedPreferences.Editor remove(String key)
- boolean commit()
SharedPreferencesはインタフェースなので、インスタンス生成はできない。 ContextのgetSharedPreferences(String name, int mode)でインスタンスを取得する。
MODE
- Context.MODE_PRIVATE - アプリ内で使用
- Context.MODE_WORLD_READABLE: 他のアプリの読み
- Context.MODE_WORLD_WRITEABLE : 他のアプリの読み書き
※ Android4.2からはセキュリティ面でContext.MODE_WORLD_READABLEとContext.MODE_WORLD_WRITEABLEは推奨しない。 データを共有する場合は、ContextProviderを利用する。
サンプルコード
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/readBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="READ" android:textAllCaps="false" android:textSize="20dp" android:onClick="clickTimePikcerBtn"/> <Button android:id="@+id/writeBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="WRITE" android:textAllCaps="false" android:textSize="20dp" android:onClick="clickTimePikcerBtn"/> <TextView android:id="@+id/show" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30dp" android:textColor="#f00"/> </LinearLayout>
package com.example.liguofeng.sharedpreference; import android.content.SharedPreferences; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import java.util.Map; public class MainActivity extends AppCompatActivity { SharedPreferences sharedPreferences; SharedPreferences.Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // SharedPreferencesインスタンス取得_Priveteモード sharedPreferences = getSharedPreferences("sampleData", MODE_PRIVATE); // Editorインスタンス取得 editor = sharedPreferences.edit(); Button write = (Button) findViewById(R.id.writeBtn); write.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { editor.putString("data1", "123456789"); editor.putString("data2", "123456789"); editor.putInt("data3", 123456789); // データコミット editor.commit(); Toast.makeText(MainActivity.this, "書き込み完了", Toast.LENGTH_LONG).show(); } }); Button read = (Button) findViewById(R.id.readBtn); read.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String text = ""; Map<String, ?> map = sharedPreferences.getAll(); for(Map.Entry<String, ?> e : map.entrySet()) { text += e.getKey() + " : " + e.getValue() + "\n"; } Toast.makeText(MainActivity.this, text, Toast.LENGTH_LONG).show(); } }); } }
※ /data/data/パッケージ名/shared_prefsに保存される。