Android:使用SharedPreferences保存和恢复';用户首选项';值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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. }

以上是关于Android:使用SharedPreferences保存和恢复';用户首选项';值的主要内容,如果未能解决你的问题,请参考以下文章