如何在设备内存中存储用户名密码

时间:2010-06-17 15:28:49

标签: android

如何在设备内存中存储用户名密码。 即使在用户关闭应用程序并返回之后,他也应该能够验证他的用户名和密码。 现在我在Eclipse中测试...所以请帮我一些指针/链接,这将允许我在Eclipse中测试并最终在Mobile上运行。

2 个答案:

答案 0 :(得分:1)

我使用SharedPreferences来存储用户名/密码,它是一个更轻量级的存储解决方案,对您的应用程序来说是安全的。

Shared Preferences in Android Dev Guide

答案 1 :(得分:-1)

嘿..我做了一个简单的演示,为你保存用户名和密码.. !!! 它存储在设备的内存中, 它创建一个文件并从该文件中保存/获取数据。 代码正在跟随..

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Login extends Activity {

    LinearLayout lymain;

    EditText user_edit;
    EditText pass_edit;

    TextView user_txt;
    TextView pass_txt;

    CheckBox savepass;

    Button save;

    public static final String PREFS_NAME = "MyPrefsFile";
    private static final String PREF_USERNAME = "username";
    private static final String PREF_PASSWORD = "password";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        lymain = new LinearLayout(getApplicationContext());
        lymain.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        lymain.setPadding(15,15,15,15);
        lymain.setOrientation(1);
        lymain.setGravity(Gravity.CENTER);

        user_edit = new EditText(getApplicationContext());
        user_edit.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

        pass_edit = new EditText(getApplicationContext());        
        pass_edit.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

        user_txt = new TextView(getApplicationContext());
        user_txt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));        
        user_txt.setText("Enter Username");

        pass_txt = new TextView(getApplicationContext());
        pass_txt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        pass_txt.setText("Enter Password");

        savepass = new CheckBox(getApplicationContext());
        savepass.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,40));
        savepass.setText("Save Username/Password?");

        save = new Button(getApplicationContext());
        save.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        save.setText(" SAVE ");

        save.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                String loginName = user_edit.getText().toString();
                String password = pass_edit.getText().toString();

                if(savepass.isChecked()){
                    getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
                        .edit()
                        .putString(PREF_USERNAME, loginName)
                        .putString(PREF_PASSWORD, password)
                        .commit();
                    Toast.makeText(getApplicationContext(),"Saved Successfully",Toast.LENGTH_LONG).show();
                }
            }
        });


        SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);   
        user_edit.setText(pref.getString(PREF_USERNAME, null));
        pass_edit.setText(pref.getString(PREF_PASSWORD, null));


        lymain.addView(user_txt);
        lymain.addView(user_edit);
        lymain.addView(pass_txt);
        lymain.addView(pass_edit);
        lymain.addView(savepass);
        lymain.addView(save);

        setContentView(lymain);       
    }
}