简体   繁体   English

Android中的SharedPreferences

[英]SharedPreferences in Android

I have used SharedPreferences to save some details in the Main Activity .Now i want to get the details in some other activity but i am not able to get them . 我已经使用SharedPreferences在Main Activity中保存了一些细节。现在我想在其他活动中获取细节,但是我无法获取它们。

Code to save 保存代码

public void saveInformation(String username,String password) {
        SharedPreferences shared = getSharedPreferences("shared", MODE_PRIVATE);
        SharedPreferences.Editor editor = shared.edit();
        editor.putString("username", username);
        editor.putString("password", password);
        editor.commit();
    }

Code to get 获取代码

SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
String username = prefs.getString("username", null);

But this is not working for me . 但这对我没有用。 How could I get his? 我怎么能得到他?

May this help you: 可以帮助您:

Buddy Change these lines in your code to get : 好友在代码中更改以下几行即可

SharedPreferences prefs = getSharedPreferences("shared", MODE_PRIVATE); 
String username = prefs.getString("username","");

得到的代码应该是

SharedPreferences prefs = getSharedPreferences("shared", MODE_PRIVATE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM