如何使用sharedPreference类存储密码?

问题描述:

我正在开发一个android应用程序,用于使用消息获取数据。密码创建这里是我的代码,我如何使用sharedPreference类来存储密码?如何使用sharedPreference类存储密码?

 public class MainActivity extends Activity { 

Button Switchon; 
EditText passwd; //button name 
String ms; 
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    passwd = (EditText) findViewById(R.id.passwd); 
    Switchon = (Button) findViewById(R.id.Switchon); 



    Switchon.setOnClickListener(new View.OnClickListener() { 



     @Override 
     public void onClick(View arg0) { 
     @SuppressWarnings("unused") 
     String ms = passwd.getText().toString(); 
      Toast.makeText(getApplicationContext(), "You have successfully created and this app is on", Toast.LENGTH_SHORT).show(); 

     } 
    }); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 



    } 
+1

所以,什么你已经尝试在SharePrefrence的代码? – 2013-03-06 10:41:45

+0

@akbari dipali是的,当按钮点击时存储密码..请帮助我....感谢 – Abhilash 2013-03-08 05:59:56

存储值在共享偏好:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
    SharedPreferences.Editor editor = preferences.edit(); 
    editor.putString("password","123456"); 
    editor.commit(); 

要从共享偏好检索值:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
    String name = preferences.getString("password",""); 
+1

添加到Nirav的答案,不要忘记哈希密码。永远不要将密码保存为纯文本。请参阅此链接以获取如何对其进行哈希处理的示例。 http://www.mkyong.com/java/java-sha-hashing-example/ – greenkode 2013-03-06 13:42:15

+0

@Umoh:我知道,这只是个例子 – 2013-03-06 14:00:10