最近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;
}
}
页面截图:

- Jquery 实现 “下次自动登录” 记住用户名密码功能
转载自:http://blog.csdn.net/aspnet_lyc/article/details/12030039?utm_source=tuicool&utm_medium=refer ...
- autohotkey 自动登录输入用户名密码 getElementsByTagName/getElementsByClassName/getElementById
针对button未设置id的.可以通过getElementsByTagName获取button的对象数组,再明确其在对象数组中的位置,如第4个button,通过[3]获取.再调用此对象的click() ...
- PLSQL自动登录,记住用户名密码
转: PLSQL自动登录,记住用户名密码&日常使用技巧 配置启动时的登录用户名和密码 这是个有争议的功能,因为记住密码会给带来数据安全的问题. 但假如是开发用的库,密码甚至可以和用户名相同,每 ...
- TortoiseGit自动记住用户名密码的方法
TortoiseGit自动记住用户名密码的方法 windows下比较比较好用的git客户端有2种: msysgit + TortoiseGit(乌龟git) GitHub for Windows gi ...
- Windows下让Git记住用户名密码(https)
最近开始跟老板共同维护公司的框架代码,于是毫不犹豫地选择了Git这个驰名的版本控制系统(公司使用的是TFS,但外网访问老是断线). 选择的托管平台是OSChina,原因是其可以新建私有项目. 在拉取和 ...
- cookie记住用户名密码
<script src="js/jquery.cookie.js" type="text/javascript"></script> $ ...
- [No00008F]PLSQL自动登录,记住用户名密码&日常使用技巧
配置启动时的登录用户名和密码 这是个有争议的功能,因为记住密码会给带来数据安全的问题. 但假如是开发用的库,密码甚至可以和用户名相同,每次输入密码实在没什么意义,可以考虑让PLSQL Develope ...
- 让Team Foundation Server/TFS自动记住用户名密码解决方案
在使用Team Foundation Server(以下简称TFS) 的时候,在每次打开Visual Studio TFS时候,需要输入用户名和秘密,比较麻烦.现提供一种方法可以解决这个问题: 依次执 ...
- TFS自动记住用户名密码
在使用Team Foundation Server(以下简称TFS) 的时候,先在安装Team Foundation 的机器中新建一个与客户机中的同名的用户名,这样,在Visual Studio 20 ...
随机推荐
- 【P1304】【P1305】选课与选课输出方案
多叉树归 原题: 学校实行学分制.每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分.学校开设了N(N<500)门的选修课程,每个学生可选课程的数量M是给定的.学生选修了这M门课并考核 ...
- spring源码学习之:spring容器的applicationContext启动过程
Spring 容器像一台构造精妙的机器,我们通过配置文件向机器传达控制信息,机器就能够按照设定的模式进行工作.如果我们将Spring容器比喻为一辆汽车,可以将 BeanFactory看成汽车的发动机, ...
- matlab 工具之各种降维方法工具包,下载及使用教程,有PCA, LDA, 等等。。。
最近跑深度学习,提出的feature是4096维的,放到我们的程序里,跑得很慢,很慢.... 于是,一怒之下,就给他降维处理了,但是matlab 自带的什么pca( ), princomp( )函数, ...
- max_input_vars 的影响
一同事,让帮忙解决问题:post了1020条数据,结果只显示250条. 判断可能是php的post设置问题,结果发现php.ini里关于post的设置没有问题. 通过 php://input 得到请求 ...
- understanding checkpoint_completion_target
Starting new blog series – explanation of various configuration parameters. I will of course follow ...
- C#中的预处理指令
C#中的预处理指令 作为预处理中的一对:#region name ,#endregion可能是大家使用得最多的,我也常用它来进行代码分块,在一个比较长的cs文件中,这么做确实是一件可以让你使代码更清晰 ...
- OSI安全体系结构
建立七层模型主要是为解决异种网络互连时所遇到的兼容性问题.它的最大优点是将服务.接口和协议这三个概念明确地区分开来;也使网络的不同功能模块分担起 不同的职责.也就是说初衷在于解决兼容性,但当网络发展到 ...
- Apache ab并发负载压力测试
由于现在网站都需要能够承受高并发要求的能力,所以当我们写完代码后,如果需要上线,最好都经过压力测试后,这样比较好 运行: 在Windows系统下,打开cmd命令行窗口,定位到apache安装目录的bi ...
- python exec
exec官方声明This statement supports dynamic execution of Python code. exec语句用来执行储存在字符串或文件中的python语句.exec ...
- SpringQtz 时间任务调度
1.配置所需要maven jar包 <!-- 任务调度需要的jar包--> <dependency> <groupId>org.quartz-scheduler&l ...