Android中的SharedPreferences [英] SharedPreferences in Android

查看:90
本文介绍了Android中的SharedPreferences的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用SharedPreferences在Main Activity中保存了一些详细信息.现在,我想在其他活动中获取详细信息,但是我无法获取它们.

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 .

要保存的代码

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();
    }

要获取的代码

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

但这对我不起作用.我怎么能得到他?

But this is not working for me . How could I get his?

推荐答案

获取的代码应为

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

这篇关于Android中的SharedPreferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆