当前位置:操作系统 > 安卓/Android >>

Android开发相关:(02)使用首选项保存活动的状态

[java] final String INITIALIZED = "initialized"; 
SharedPreferences myPrefs = getPreferences(MODE_PRIVATE); 
 
boolean hasPreferences = myPrefs.getBoolean(INITIALIZED, false); 
if (hasPreferences) { 
    Log.v("Preferences", "We've been called brefore"); 
    // Read other values as desired from preferences file...  
    someString = myPrefs.getString("someString", ""); 
} else { 
    Log.v("Preferences", "First time ever being called"); 
    // Set up initial values for what will end up   
    // in the preferences file  
    someString = "Some default value"; 

// Later when ready to write out values  
Editor editor = myPrefs.edit(); 
editor.putBoolean(INITIALIZED, true); 
editor.putString("someString", someString); 
// Write other values as desired  
editor.commit(); 



摘自 horsttnann的专栏
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,