保存登陆username和password
在一些软件中登陆时保存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的更多相关文章
- 最终结算“Git Windowsclient保存username与password”问题
Git - How to use netrc file on windows - Stack Overflow 这就是正确答案,我们已经验证过了,以下具体描写叙述一下解决方法: 1. 在Windows ...
- 问题: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 ...
- 解决git Push时请求username和password,而不是ssh-key验证
转载自:https://blog.lowstz.org/posts/2011/11/23/why-git-push-require-username-password-github/ 之前开始用git ...
- redmine忘记username和password
环境: Ubuntu 13.10 bitnami-redmine-2.5.1-1-linux-x64-installer.run 用bitnami安装完redmine以后,有是否忘记了username ...
- 给新手--安装tomcat后username和password设置以及项目怎么部署在tomcatserver上
安装后tomcatserver后.登陆首先就是让输入username和password.但是我们在安装tomcat的过程中好像没有让设置username和password,这时候可能有人就抓狂了.还有 ...
- Incorrect username or password ( access token )解决
Q:Git提交时,给出提示Incorrect username or password ( access token ) K: 此处是用户名或者密码有误,建议解决方法两种.具体看哪一种可行,可试. 第 ...
- Docker:unauthorized: incorrect username or password.
用VS2017编译DockerCompose项目,显示错误:unauthorized: incorrect username or password. 打开命令行工具,输入docker login命令 ...
- TortoiseGit 连接oschina不用每次输入username和password的方法
每次git clone 和push 都要输入username和password.尽管安全.但在本机上每次都输有些麻烦,怎样记住username和password呢? 在网上看了各种方法,太杂,非常多可 ...
- tomcatserver管理界面username和password忘记
tomcatserverhttp://localhost:8080/ 这样訪问,点击Manager App后要求输入username和password才干进入管理应用界面 我忘记了username和p ...
随机推荐
- 洛谷——P1680 奇怪的分组
P1680 奇怪的分组 题目背景 终于解出了dm同学的难题,dm同学同意帮v神联络.可dm同学有个习惯,就是联络同学的时候喜欢分组联络,而且分组的方式也很特别,要求第i组的的人数必须大于他指定的个数c ...
- Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises) D. Jury Meeting(双指针模拟)
D. Jury Meeting time limit per test 1 second memory limit per test 512 megabytes input standard inpu ...
- Codeforces Round #277 (Div. 2) D. Valid Sets (DP DFS 思维)
D. Valid Sets time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 北邮校赛 F. Gabriel's Pocket Money(树状数组)
F. Gabriel's Pocket Money 2017- BUPT Collegiate Programming Contest - sync 时间限制 2000 ms 内存限制 65536 K ...
- 使用ICSharpCode.SharpZipLib+Aspose模板批量导出Word
由于是Web端的项目,所以点击按钮之后直接从Aspose模板读取数据,然后在内存中操作,而不是下载到本地后再打包弄到内存中下载.废话不多说,直接上代码 public ActionResult Expo ...
- hdu1006 Tick and Tick (数学题 借鉴了大神的博客)
先缩短一半的时间:早上的12个小时和下午的12小时对时钟是一样的,因为时钟12小时与0小时的三针位置相同.接着就是了解到每次所有的针从有重合到再次有重合至多有一段连续的段符合三针分离度大于n.所以只要 ...
- 初见Python<2>:列表和元组
1.在python中,最基本的数据结构是序列,序列中每一个元素被分配一个序号,即元素的位置,称为索引.索引从0开始,-1表示倒数第一个元素,-2表示倒数第二个元素,因此既可以是从前到后开始对元素进 ...
- Gauss 消元(模板)
/* title:Gauss消元整数解/小数解整数矩阵模板 author:lhk time: 2016.9.11 没学vim的菜鸡自己手打了 */ #include<cstdio> #in ...
- 【BFS】The Morning after Halloween
[POJ3523]The Morning after Halloween Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 23 ...
- Codeforces Beta Round #3 C. Tic-tac-toe 模拟题
C. Tic-tac-toe 题目连接: http://www.codeforces.com/contest/3/problem/C Description Certainly, everyone i ...