Android应用程序存储和调用用户名和密码

时间:2016-01-27 19:37:18

标签: java android

我有一个应用程序,我想创建一个登录活动。我有 1)登录(按钮) 2)UserID(EditText) 3)密码(EditText)

我想将用户名和密码存储在设备的内部存储中。如何创建SharedPreferences从中提取并加密的文件?

login_Button.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {

            SharedPreferences userDetails = getSharedPreferences("userdetails", MODE_PRIVATE);
            SharedPreferences.Editor edit = userDetails.edit();
            edit.clear();

            if(etUserID.getText().toString().equals("")){
                Toast.makeText(getApplicationContext(),"Please enter a User ID", Toast.LENGTH_LONG).show();
            }else if (etPassword.getText().toString().equals("")){
                Toast.makeText(getApplicationContext(),"Please enter a password", Toast.LENGTH_LONG).show();
            }else{
                edit.putString("userID", txtUID.getText().toString().trim());
                edit.putString("password", txtPass.getText().toString().trim());
                edit.commit();
            }



        }
    });

txtUID和txtPass显示为红色,当我删除它时,系统说getText是错误的。

这是我第一次认证。我从android开发页面找到了一个名为PasswordAuthentication的东西,但我找不到任何教程或使用它的代码。

SharedPreferences userDetails = getSharedPreferences("userdetails", MODE_PRIVATE);
    String UID = userDetails.getString("userID", "");
    String pass = userDetails.getString("password", "");

    if (userDetails.contains("userID")||userDetails.contains("password")){
        startActivity(new Intent(LogOn.this,CrimeMap.class));
    }

我用于研究的资料来源: http://developer.android.com/reference/java/net/PasswordAuthentication.html how to use getSharedPreferences in android

更新

好的,这是我更新的代码。如果我没有输入用户名或密码,它会引发我的陈述。但是,当我输入正确的UserID和密码时,程序不会进入下一个活动。我将首先运行它然后继续加密它。

@覆盖     protected void onCreate(Bundle savedInstanceState){         super.onCreate(savedInstanceState);         的setContentView(R.layout.activity_log_on);

    etUserID = (EditText) findViewById(R.id.etUserID);
    etPassword = (EditText) findViewById(R.id.etPassword);
    login_Button = (Button) findViewById(R.id.bLogin);


    login_Button.setOnClickListener(new View.OnClickListener(){


        @Override
        public void onClick(View v) {

            int uid = 1155;
            String pass = "pass";

            SharedPreferences userDetails = getSharedPreferences(User_File, MODE_PRIVATE);
            SharedPreferences.Editor edit = userDetails.edit();

            edit.putInt("userID", uid);
            edit.putString("password", pass);
            edit.commit();


            if((etUserID.getText().toString().equals("")) || (etPassword.getText().toString().equals(""))) {
                Toast.makeText(getApplicationContext(),"Please enter a User ID", Toast.LENGTH_LONG).show();
            }else if (etPassword.getText().toString().equals("")){
                Toast.makeText(getApplicationContext(),"Please enter a password", Toast.LENGTH_LONG).show();
            }else{
                edit.putInt("userID", Integer.parseInt(etUserID.getText().toString()));
                edit.putString("password", etPassword.getText().toString());
            }



        }
    });

    SharedPreferences userDetails = getSharedPreferences(User_File, MODE_PRIVATE);

    if (userDetails.equals(etUserID) && (userDetails.equals(etPassword))){
        startActivity(new Intent(LogOn.this,CrimeMap.class));
    }

更新

我终于明白了!谢谢你的帮助!这是我对这部分项目的完整代码。我还没有加密它,但我想我会休息一下,稍后再回来。谢谢你的帮助!

     @Override
        public void onClick(View v) {

            int uid = 1155;
            String pass = "pass";

            SharedPreferences userDetails = getSharedPreferences(User_File, MODE_PRIVATE);
            SharedPreferences.Editor edit = userDetails.edit();

            edit.putInt("userID", uid);
            edit.putString("password", pass);
            edit.commit();


            if((etUserID.getText().toString().equals(""))){
                Toast.makeText(getApplicationContext(),"Please enter a User ID", Toast.LENGTH_LONG).show();
            }else if (etPassword.getText().toString().equals("")){
                Toast.makeText(getApplicationContext(),"Please enter a password", Toast.LENGTH_LONG).show();
            }else{
                String user_id = etUserID.getText().toString();
                int user_id2 = Integer.parseInt(user_id);
                String user_password = etPassword.getText().toString();

                int userID = userDetails.getInt("userID", 1);
                String password = userDetails.getString("password", "no name");

                if (userID == user_id2 && password.equals(user_password)){
                    startActivity(new Intent(LogOn.this,CrimeMap.class));
                }
            }

1 个答案:

答案 0 :(得分:1)

请勿尝试访问该文件。让android做到这一点,并加密您存储到共享首选项的数据。我的意思是:

  edit.putString("userID", encrypt(txtUID.getText().toString().trim()));
  edit.putString("password", encrypt(txtPass.getText().toString().trim()));

你可以使用你喜欢的任何加密,但当然你需要以这种方式解码它。

另一个选项是存储SHA1加密密码,在验证中,您将输入的SHA1加密密码与存储的字符串进行比较。您可以使用http://mvnrepository.com/artifact/commons-codec/commons-codec例如:

String sha1Password = DigestUtils.sha1Hex(password);
edit.putString("sha1Password", encrypt(sha1Password);

修复" red"问题你需要声明txtUID,txtPass。它们应该以某种方式添加到您的布局中。可能是通过编辑布局xml。但即使是以编程方式,您也可以这样做:Input text dialog Android