Android通过SharedPreferences实现自动登录记住用户名和密码功能

Android通过SharedPreferences实现自动登录记住用户名和密码功能,第1张

概述最近Android项目需要一个自动登录功能,完成之后,特总结一下,此功能依靠SharedPreferences进行实现。

最近AndroID项目需要一个自动登录功能,完成之后,特总结一下,此功能依靠SharedPreferences进行实现。

SharedPreferences简介

SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息。其存储位置在/data/data/<包名>/shared_prefs目录下。SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象实现。

SharedPreferences使用实例:记住用户名密码自动登录

大致了解了SharedPreference之后,接下来看个记住用户名密码自动登录的例子:

package com.dt5000.ischool.activity; import androID.annotation.Suppresslint; import androID.content.Context; import androID.content.Intent; import androID.content.SharedPreferences; import androID.content.SharedPreferences.Editor; import androID.os.Bundle; import androID.util.Log; import androID.vIEw.KeyEvent; import androID.vIEw.VIEw; import androID.Widget.CheckBox; import androID.Widget.EditText; import com.dt5000.ischool.util.DTUtil; import com.dt5000.ischool.util.MyApplication; /**  * @author: duanyr  * @创建时间: 2012-11-13 下午2:36:47  *  * @类说明:登录界面  */ @Suppresslint("WorldReadablefiles") public class LoginActivity extends DTUtil {   private static final String TAG = "用户登录";   private EditText username;   private EditText password;   private CheckBox autoLogin;   private SharedPreferences sharedPreferences;   private String message;   @OverrIDe   public voID onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     MyApplication.getInstance().addActivity(this);     sharedPreferences = this.getSharedPreferences("userInfo",Context.MODE_WORLD_READABLE);     if (sharedPreferences.getBoolean("auto_ISCHECK",false)) {       Intent intent = new Intent();       intent.setClass(LoginActivity.this,MainActivity.class);       startActivity(intent);     } else {       setContentVIEw(R.layout.activity_login);       initVIEw();     }   }   /**    * 初始化视图控件    */   public voID initVIEw() {     Log.i(TAG,"初始化视图控件");     username = (EditText) findVIEwByID(R.ID.username);     password = (EditText) findVIEwByID(R.ID.password);     autoLogin = (CheckBox) findVIEwByID(R.ID.autoLogin);     // 默认记住用户名     username.setText(sharedPreferences.getString("username",""));   }   /**    * 点击登录按钮时触发的方法    * @param vIEw    */   public voID userLogin(VIEw vIEw) {     String usernameString = username.getText().toString();     String passwordString = password.getText().toString();     if (valIDateUser(usernameString,passwordString)) {       Editor editor = sharedPreferences.edit();       editor.putString("username",usernameString);       if (autoLogin.isChecked()) {// 自动登录         editor.putString("password",passwordString);         editor.putBoolean("auto_ISCHECK",true).commit();       }       editor.commit();       Intent intent = new Intent();       intent.setClass(LoginActivity.this,MainActivity.class);       startActivity(intent);     } else {       alert(this,message);     }   }   //游客登录   public voID visitorLogin(VIEw vIEw) {     Intent intent = new Intent();     intent.setClass(LoginActivity.this,MainActivity.class);     startActivity(intent);   }   /**    * 验证用户名密码是否正确    *    * @param username    * @param password    * @return    */   public boolean valIDateUser(String username,String password) {     Boolean flag = false;     try {       //...此处为调用web服务,验证用户名密码的服务,特此省略       flag = true;     } catch (Exception e) {       // Todo auto-generated catch block       Log.e(TAG,e.getMessage());       message = "连接服务器失败";     }     return flag;   }   /**    * 点击退出按钮时触发的方法    */   public voID logout_Listener(VIEw vIEw) {     dialog_Exit(this);   }   /**    * 监听返回按钮,此为登录界面再返回的话给出退出提示    */   public boolean onKeyDown(int keyCode,KeyEvent event) {     if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {       dialog_Exit(this);       return false;     }     return false;   } } 

页面布局截图:

生成的配置文件位置和代码

userInfo.xml的具体代码如下:

<?xml version='1.0' enCoding='utf-8' standalone='yes' ?> <map> <string name="username">777</string> <string name="password">111111</string> <boolean name="auto_ISCHECK" value="true" /> </map> 

以上所述是小编给大家介绍的AndroID通过SharedPreferences实现自动登录记住用户名和密码功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Android通过SharedPreferences实现自动登录记住用户名和密码功能全部内容,希望文章能够帮你解决Android通过SharedPreferences实现自动登录记住用户名和密码功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1145851.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存