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

Android SharedPreferences

編輯:關於Android編程

1.獲取SharedPreferences對象

SharedPreferencesmSharedPreferences = context.getSharedPreferences(SP_NAME,Activity.MODE_PRIVATE);

2.往SharedPreferences裡面寫入數據

SharedPreferences.Editoreditor = mSharedPreferences.editor();

editor.putInt(Intkey,Intvalue);

editor.putFloat(Intkey,Intvalue);

editor.commit();

3.從SharedPreferences裡面讀取數據

int defValue =0;

int value = mSharedPreferences.getInt(Intkey, defValue);

 

4.跨進程讀取SharedPreference

Context context = null;

try {

context= this.createPackageContext("com.mediatek.oobe",Context.CONTEXT_IGNORE_SECURITY);

} catch (NameNotFoundException e) {

e.printStackTrace();

}

SharedPreferences sp =context.getSharedPreferences("com.mediatek.oobe",Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);

 

int version =sp.getInt("ota_version", -1);

SharedPreferences.Editor editor =sp.edit();

 

editor.putInt("ota_version", 0);

editor.commit();

int version1 =sp.getInt("ota_version", -1);

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved