Android(使用SharedPreferences保存和恢复'用户首选项'值)

【Android(使用SharedPreferences保存和恢复';用户首选项';值)】怀抱观古今,寝食展戏谑。这篇文章主要讲述Android:使用SharedPreferences保存和恢复';用户首选项';值相关的知识,希望能为你提供帮助。

  1. SharedPreferences prefs = getApplicationContext( ) .getSharedPreferences( " YourAppName" , 0) ;
  2. // YourAppName is just a string to identify the prefs. 0 means other apps can't see your prefs.
  3. prefs.getString( " lastUrl" , " " ) ; // get out a string called 'lastUrl'. " " is the return value if the preference doesn't exist
  4.  
  5. // To change values you need an 'Editor'
  6. Editor edit = prefs.edit( ) ;
  7. edit.putString( " lastUrl" , " http://www.example.com" ) ; // save the value..
  8.  
  9. // could make multiple changes here.
  10. if ( !edit.commit( ) ) {
  11. Log.e( " FOO" , " Failed to save prefs" ) ;
  12. }


    推荐阅读