在一些软件中登陆时保存username和password是常见的功能,它实现起来也特别简单,其原理就是在点击登陆button时推断是否勾选保存password选项,假设勾选,则在内存中保存一份包括username和password的文件文件,在下次再打开登陆界面时会获取文件里的信息。

登陆界面:

在onclick中推断假设勾选了记住password:

			if (cb_remeber_password.isChecked()) {
boolean result = LoginService.saveInfo(this, username, password);
if(result) {
Toast.makeText(this, "保存密码成功", 0).show();
}

saveInfo的方法:

	public static boolean saveInfo(Context context, String username,
String password) {
//getFileDir : /data/data/包名/files
//getCacheDir : /data/data/包名/cache
File file = new File(context.getFilesDir(), "info.txt"); try {
FileOutputStream fos = new FileOutputStream(file);
fos.write((username + "#" + password).getBytes());
fos.flush();
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

在这里的getFileDir获取的是手机内存的文件下路径,getCacheDir获取的是应用缓存路径,放在这个路径下的文件会在手机清理缓存是被清理,并且有限制大小,所以一般不建议放在getCacheDir路径下。

这样就保存了一份包括实username和password信息的文件了。下次登录时就能够直接获取这里面的信息而不用又一次输入了

		HashMap<String, String> info = LoginService.getInfo(this);
if(info != null) {
et_username.setText(info.get("username"));
et_password.setText(info.get("password"));
}

获取登录信息getInfo方法:

	public static HashMap<String, String> getInfo(Context context) {
File file = new File(context.getFilesDir(), "info.txt");
try {
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String[] result = br.readLine().split("#");
HashMap<String, String> map = new HashMap<String, String>();
map.put("username", result[0]);
map.put("password", result[1]);
br.close();
return map; } catch (Exception e) {
Toast.makeText(context, "无法读取用户信息", 0).show(); }
return null;
}

这样就实现了登录信息的获取

再次登录时的状态:

保存登陆username和password的更多相关文章

  1. 最终结算“Git Windowsclient保存username与password”问题

    Git - How to use netrc file on windows - Stack Overflow 这就是正确答案,我们已经验证过了,以下具体描写叙述一下解决方法: 1. 在Windows ...

  2. 问题:docker pull 用户登陆tricky,Error response from daemon: unauthorized: incorrect username or password

    问题描述: PS C:\WINDOWS\system32> docker pull rabbitmqUsing default tag: latest Please login prior to ...

  3. 解决git Push时请求username和password,而不是ssh-key验证

    转载自:https://blog.lowstz.org/posts/2011/11/23/why-git-push-require-username-password-github/ 之前开始用git ...

  4. redmine忘记username和password

    环境: Ubuntu 13.10 bitnami-redmine-2.5.1-1-linux-x64-installer.run 用bitnami安装完redmine以后,有是否忘记了username ...

  5. 给新手--安装tomcat后username和password设置以及项目怎么部署在tomcatserver上

    安装后tomcatserver后.登陆首先就是让输入username和password.但是我们在安装tomcat的过程中好像没有让设置username和password,这时候可能有人就抓狂了.还有 ...

  6. Incorrect username or password ( access token )解决

    Q:Git提交时,给出提示Incorrect username or password ( access token ) K: 此处是用户名或者密码有误,建议解决方法两种.具体看哪一种可行,可试. 第 ...

  7. Docker:unauthorized: incorrect username or password.

    用VS2017编译DockerCompose项目,显示错误:unauthorized: incorrect username or password. 打开命令行工具,输入docker login命令 ...

  8. TortoiseGit 连接oschina不用每次输入username和password的方法

    每次git clone 和push 都要输入username和password.尽管安全.但在本机上每次都输有些麻烦,怎样记住username和password呢? 在网上看了各种方法,太杂,非常多可 ...

  9. tomcatserver管理界面username和password忘记

    tomcatserverhttp://localhost:8080/ 这样訪问,点击Manager App后要求输入username和password才干进入管理应用界面 我忘记了username和p ...

随机推荐

  1. ZOJ 3949 Edge to the Root(想法)(倍增)

    Edge to the Root Time Limit: 1 Second      Memory Limit: 131072 KB Given a tree with n vertices, we ...

  2. 计算机基础-day2

    为何要有操作系统? 首先要说明一下操作系统是什么? 操作系统是协调.管理.控制计算机硬件和为应用程序提供接口的控制软件,操作系统向下连接硬件,向上承载应用程序. 由于硬件不论种类还是工作流程都是繁杂的 ...

  3. 【DFS】【拓扑排序】【动态规划】Gym - 100642A - Babs' Box Boutique

    给你10个箱子,有长宽高,每个箱子你可以决定哪个面朝上摆.把它们摞在一起,边必须平行,上面的不能突出来,问你最多摆几个箱子. 3^10枚举箱子用哪个面.然后按长为第一关键字,宽为第二关键字,从大到小排 ...

  4. 【二分】Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market

    傻逼二分 #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; ll ...

  5. jquery ajax 的封装

    var tooAjaxData = new Object(); tooAjaxData = function () { this.AjaxUrl =" ";}; bookInfoC ...

  6. Implicit declaration of function 'CC_MD5' is invalid in C99

    //导入这个就行了#import <CommonCrypto/CommonDigest.h> //没有导包的时候,提示如下: Implicit declaration of functio ...

  7. Oracle Linux logoOracle Linux

    http://www.oschina.net/p/oracle_enterprise_linux

  8. php-scandir()报错

    l       linux下 vim /usr/local/php/etc/php.in l       直接斜杠找 /disable_functions   回车 l       按i键 l     ...

  9. __FILE__,__LINE__,FUNCTION__实现代码跟踪调试

    转:http://www.cnitblog.com/zouzheng/archive/2007/08/31/32691.aspx 先看下简单的初始代码:注意其编译运行后的结果. root@xuanfe ...

  10. express结合jade模板渲染HTML

    在线html转jade工具>> 注意:以下是在Windwo环境下 运行: npm install jade 然后你的目录node_modules下将增加jade文件夹 app.js var ...