在很多的AndroID项目中都需要用户登录、注册。这样的话在开发中做好保护用户密码的工作就显得尤为重要。这里我把自己的密码保护方法记录下来。
这是我建了一个保存密码的文件,以便于检查自己保存密码或者上传到服务器的时候密码是否已经被保护了。这就是当我输入用户名和密码之后点击记住密码之后
保存在SD卡上的文件,打开之后可以明显的看到密码已经被保护了。
下面是我的布局文件以及主程序的代码:
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="#E6E6E6" androID:orIEntation="vertical"> <ImageVIEw androID:ID="@+ID/iv_head" androID:layout_wIDth="150dp" androID:layout_height="150dp" androID:layout_centerHorizontal="true" androID:layout_margintop="40dp" androID:src="@drawable/ic_launcher"/> <linearLayout androID:ID="@+ID/layout" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_below="@+ID/iv_head" androID:layout_margin="10dp" androID:background="#FFFFFF" androID:orIEntation="vertical"> <relativeLayout androID:ID="@+ID/rl_username" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_margin="10dp"> <TextVIEw androID:ID="@+ID/tv_name" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_centerVertical="true" androID:text="账号"/> <EditText androID:ID="@+ID/et_number" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_marginleft="5dp" androID:layout_toRightOf="@+ID/tv_name" androID:background="@null"/> </relativeLayout> <VIEw androID:layout_wIDth="match_parent" androID:layout_height="2dp" androID:background="#E6E6E6"/> <relativeLayout androID:ID="@+ID/rl_userpsd" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_margin="10dp"> <TextVIEw androID:ID="@+ID/tv_psw" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_centerVertical="true" androID:text="密码"/> <EditText androID:ID="@+ID/et_password" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_marginleft="5dp" androID:layout_toRightOf="@+ID/tv_psw" androID:inputType="textPassword" androID:background="@null"/> </relativeLayout> </linearLayout> <button androID:ID="@+ID/login" androID:onClick="login" androID:layout_wIDth="match_parent" androID:layout_height="40dp" androID:layout_below="@+ID/layout" androID:layout_centerHorizontal="true" androID:layout_marginleft="10dp" androID:layout_marginRight="10dp" androID:layout_margintop="20dp" androID:background="#3C8DC4" androID:text="登录" androID:textcolor="#FFFFFF"/> <button androID:ID="@+ID/signUp" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="注册" androID:background="#E6E6E6" androID:textcolor="#000000" androID:layout_margintop="21dp" androID:layout_centerHorizontal="true" androID:layout_marginRight="10dp" androID:layout_below="@+ID/login" androID:layout_alignParentRight="true"/> <CheckBox androID:ID="@+ID/save" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignBaseline="@+ID/signUp" androID:layout_alignBottom="@+ID/signUp" androID:layout_alignleft="@+ID/login" androID:layout_marginleft="14dp" androID:text="记住密码" /> </relativeLayout>
package com.itcast.test03;import java.io.BufferedReader;import java.io.IOException;import java.io.inputStream;import java.io.inputStreamReader;import java.io.UnsupportedEnCodingException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.ArrayList;import java.util.List;import org.apache.http.httpentity;import org.apache.http.httpResponse;import org.apache.http.nameValuePair;import org.apache.http.ParseException;import org.apache.http.clIEnt.ClIEntProtocolException;import org.apache.http.clIEnt.httpClIEnt;import org.apache.http.clIEnt.entity.UrlEncodedFormEntity;import org.apache.http.clIEnt.methods.httpPost;import org.apache.http.impl.clIEnt.DefaulthttpClIEnt;import org.apache.http.message.BasicnameValuePair;import org.apache.http.util.EntityUtils;import org.Json.JsONArray;import org.Json.JsONObject;import androID.os.Build;import androID.os.Bundle;import androID.os.StrictMode;import androID.annotation.Suppresslint;import androID.annotation.TargetAPI;import androID.app.Activity;import androID.content.Intent;import androID.content.SharedPreferences;import androID.text.TextUtils;import androID.util.Log;import androID.vIEw.Menu;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.vIEw.VIEw.OnFocuschangelistener;import androID.Widget.button;import androID.Widget.CheckBox;import androID.Widget.EditText;import androID.Widget.Toast;public class MainActivity extends Activity implements OnClickListener { private EditText et_username; private EditText et_userPsd; private button login; private button signUp; private CheckBox save; private String user,pass; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); et_username = (EditText)findVIEwByID(R.ID.et_number); et_userPsd = (EditText)findVIEwByID(R.ID.et_password); login=(button)findVIEwByID(R.ID.login); signUp=(button)findVIEwByID(R.ID.signUp); save = (CheckBox)findVIEwByID(R.ID.save); save.setonClickListener(new CheckBox.OnClickListener(){ public voID onClick(VIEw v) { SharedPreferences pre = getSharedPreferences("loginvalue",MODE_WORLD_WRITEABLE); pass = MD5( et_userPsd.getText().toString()); user = et_username.getText().toString(); if (!pass.equals("") && !user.equals("")) { pre.edit().putString("username",et_username.getText().toString()) .putString("password",encryptmd5(pass)).commit(); Toast.makeText(getApplicationContext(),"保存成功!",Toast.LENGTH_SHORT).show(); } else{ Toast.makeText(getApplicationContext(),"密码不能为空!",Toast.LENGTH_LONG).show(); } } }); login.setonClickListener(new button.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { SharedPreferences sp = getSharedPreferences("loginvalue",MODE_WORLD_READABLE); String loginuser = sp.getString("username",null); String loginpass = sp.getString("password",null); user = et_username.getText().toString(); pass = et_userPsd.getText().toString(); String passmd5 = MD5(pass); String encryptmd5 = encryptmd5(passmd5); System.out.println("username="+ loginuser + "-------------password="+ loginpass); System.out.println("user=="+ user + "-------------encryptmd5=="+ encryptmd5); if (!user.equals("") && !pass.equals("")) { if (user.equals(loginuser) && encryptmd5.equals(loginpass)) { Intent intent = new Intent(); intent.setClass(MainActivity.this,StudentMainActivity.class); MainActivity.this.startActivity(intent); finish(); } else{ Toast.makeText(getApplicationContext(),"密码是错误的!",Toast.LENGTH_LONG).show(); } } else{ Toast.makeText(getApplicationContext(),Toast.LENGTH_LONG).show(); } } }); initWidget();// } private voID initWidget() { login.setonClickListener(this); signUp.setonClickListener(this); et_username.setonFocuschangelistener(new OnFocuschangelistener() { @OverrIDe public voID onFocusChange(VIEw v,boolean hasFocus) { // Todo auto-generated method stub if(!hasFocus){ String username=et_username.getText().toString().trim(); if(username.length()<4){ Toast.makeText(MainActivity.this,"用户名不能小于4个字符",Toast.LENGTH_SHORT); } } } }); et_userPsd.setonFocuschangelistener(new OnFocuschangelistener() { @OverrIDe public voID onFocusChange(VIEw v,boolean hasFocus) { // Todo auto-generated method stub if(!hasFocus){ String password=et_userPsd.getText().toString().trim(); if(password.length()<4){ Toast.makeText(MainActivity.this,"密码不能小于4个字符",Toast.LENGTH_SHORT); } } } }); } public voID onClick(VIEw v) { // Todo auto-generated method stub switch(v.getID()) { case R.ID.login: if(checkEdit()) { login(); } break; case R.ID.signUp: Intent intent2=new Intent(MainActivity.this,SignUp.class); startActivity(intent2); break; } } private boolean checkEdit(){ if(et_username.getText().toString().trim().equals("")){ Toast.makeText(MainActivity.this,"用户名不能为空",Toast.LENGTH_SHORT).show(); Intent intent=new Intent(MainActivity.this,StudentMainActivity.class); startActivity(intent); }else if(et_userPsd.getText().toString().trim().equals("")){ Toast.makeText(MainActivity.this,"密码不能为空",Toast.LENGTH_SHORT).show(); }else{ return true; } return false; } private voID login(){ //这个网址需要改动 String httpUrl="http://192.168.1.102:8080/web-test/login.Jsp"; httpPost httpRequest=new httpPost(httpUrl); List<nameValuePair> params=new ArrayList<nameValuePair>(); params.add(new BasicnameValuePair("username",et_username.getText().toString().trim())); params.add(new BasicnameValuePair("password",et_userPsd.getText().toString().trim())); httpentity httpentity = null; try { httpentity = new UrlEncodedFormEntity(params,"utf8"); } catch (UnsupportedEnCodingException e) { // Todo auto-generated catch block e.printstacktrace(); } httpRequest.setEntity(httpentity); httpClIEnt httpclIEnt=new DefaulthttpClIEnt(); httpResponse httpResponse = null; try { httpResponse = httpclIEnt.execute(httpRequest); } catch (ClIEntProtocolException e) { // Todo auto-generated catch block e.printstacktrace(); } catch (IOException e) { // Todo auto-generated catch block e.printstacktrace(); } if(httpResponse.getStatusline().getStatusCode()==200) { String strResult = null; try { strResult = EntityUtils.toString(httpResponse.getEntity()); } catch (ParseException e) { // Todo auto-generated catch block e.printstacktrace(); } catch (IOException e) { // Todo auto-generated catch block e.printstacktrace(); } Toast.makeText(MainActivity.this,strResult,StudentMainActivity.class); startActivity(intent); } else { Toast.makeText(MainActivity.this,"登录失败!",Toast.LENGTH_SHORT).show(); } } public static String MD5(String str){ MessageDigest md5 = null; try { md5 = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { // Todo auto-generated catch block e.printstacktrace(); return ""; } char[] chararray = str.tochararray(); byte[] byteArray = new byte[chararray.length]; for (int i = 0; i < chararray.length; i++) { byteArray[i] = (byte)chararray[i]; } byte[] md5Bytes = md5.digest(byteArray); StringBuffer hexValue = new StringBuffer(); for (int i = 0; i < md5Bytes.length; i++) { int val = ((int)md5Bytes[i])&0xff; if(val<16){ hexValue.append("0"); } hexValue.append(Integer.toHexString(val)); } return hexValue.toString(); } public static String encryptmd5(String str){ char[] a = str.tochararray(); for (int i = 0; i < a.length; i++) { a[i] = (char)(a[i]^'1'); } String s = new String(a); return s; } }
添加权限:
<uses-permission androID:name="androID.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission androID:name="androID.permission.INTERNET" />
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android登录时密码保护功能全部内容,希望文章能够帮你解决Android登录时密码保护功能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)